记录在使用selenium过程中踩的坑------

在使用selenium时,用click点击网站弹出的文件上传框的“上传文件”按钮不生效,报错selenium.common.exceptions.InvalidArgumentException

log如下:

test_xxxxxx.py::test_xxxxxxx FAILED [100%]Traceback (most recent call last):
File "F:\xxxxxxxx\page\BasePage.py", line 20, in box_click
element.click()
File "F:\Python\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "F:\Python\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "F:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "F:\Python\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument
(Session info: chrome=78.0.3904.97)

元素的定位是这样: Select_File = (By.CSS_SELECTOR, "input[type=file]")

解决方法:

    def action_click(self, locator):
temp = 1
log = self._log
while temp < 5:
try:
element = WebDriverWait(self._driver, 5, 1).until(expected_conditions.element_to_be_clickable(locator))
# element.click()
ActionChains(self._driver).click(element).perform()
except Exception as e:
time.sleep(0.5)
temp += 1
# traceback.print_exc()
log.logger.debug(traceback.print_exc())
else:
break
# 尝试5次仍失败,则终止
if temp == 5:
self.save_screen()
raise Exception("Fail to click")

element.click()改为 ActionChains(self._driver).click(element).perform()

原先用的click方法是
File "F:\Python\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
换为ActionChains的

至于为什么第一种方式不可以,现在还没弄懂。

引申:

两种Click有什么不同?

下面是我的一点小见解。

我大致看了一下源码,第一种方式是对远程的web driver post 一个请求/session/$sessionId/element/$id/click 去进行click操作

Communicates with the Remote WebDriver server using the WebDriver wire protocol

https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol

Python\lib\site-packages\selenium\webdriver\remote\webelement.py

def click(self):

"""Clicks the element."""

self._execute(Command.CLICK_ELEMENT)

Python\Lib\site-packages\selenium\webdriver\remote\remote_connection.py

Command.CLICK_ELEMENT: ('POST', '/session/$sessionId/element/$id/click')

第二种方式是模拟鼠标操作,对元素进行点击,再post /session/$sessionId/actions

Python\Lib\site-packages\selenium\webdriver\common\action_chains.py

ActionChains are a way to automate low level interactions such as

mouse movements, mouse button actions, key press, and context menu interactions.

This is useful for doing more complex actions like hover over and drag and drop.

    def click(self, on_element=None):
"""
Clicks an element. :Args:
- on_element: The element to click.
If None, clicks on current mouse position.
"""
if on_element:
self.move_to_element(on_element)
if self._driver.w3c:
self.w3c_actions.pointer_action.click()
self.w3c_actions.key_action.pause()
self.w3c_actions.key_action.pause()
else:
self._actions.append(lambda: self._driver.execute(
Command.CLICK, {'button': 0}))
return self

Python\Lib\site-packages\selenium\webdriver\common\actions\action_builder.py

    def perform(self):
enc = {"actions": []}
for device in self.devices:
encoded = device.encode()
if encoded['actions']:
enc["actions"].append(encoded)
self.driver.execute(Command.W3C_ACTIONS, enc)

Command.W3C_ACTIONS: ('POST', '/session/$sessionId/actions')

Selenium click不生效 报错selenium.common.exceptions.InvalidArgumentException的更多相关文章

  1. 【Selenium】【BugList7】执行driver.find_element_by_id("kw").send_keys("Selenium"),报错:selenium.common.exceptions.InvalidArgumentException: Message: Expected [object Undefined] undefined to be a string

    [版本] selenium:3.11.0 firefox:59.0.3 (64 位) python:3.6.5 [代码] #coding=utf-8 from selenium import webd ...

  2. robotframework执行用例时,报错selenium.common.exceptions.WebDriverException: Message: unknown error: cannot get automation extension from unknown error: page could not be found: chrome-extension://aapnijgdinl

    在用robotframework编写移动端测试用例(用chrome浏览器模拟手机浏览器),执行用例时, 报错selenium.common.exceptions.WebDriverException: ...

  3. selenium调用Firefox和Chrome需要注意的一些问题,和出现的报错selenium:expected [object undefined] undefined to be a string

    在高版本selenium下如:selenium3.4.3 1.高版本的selenium需要浏览器安装一些补丁驱动 Firefox:geckodriver 下载网址:http://download.cs ...

  4. 解决selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid 'expiry'

    解决selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid 'expiry'   ...

  5. windows下使用selenium报错selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH

    问题 :执行程序代码报错: WebDriverException:Message:'geckodriver'executable needs to be in Path 或者 selenium.com ...

  6. python无法启动火狐浏览器且报错“selenium.common.exceptions.WebDriverException: Message: Unable to find a matching set of capabilities”

    安装了python2,使用pip安装了selenium,但是在使用时,报了“selenium.common.exceptions.WebDriverException: Message: 'gecko ...

  7. python+selenium遇到元素定位不到的问题,顺便记录一下自己这次的错误(报错selenium.common.exceptions.NoSuchElementException)

    今天在写selenium一个发送邮件脚本时,遇到一些没有找到页面元素的错误.经过自己反复调试,找原因百度,终于解决了.简单总结一下吧,原因有以下几点: 一:Frame控件嵌套,.Frame/Ifram ...

  8. python selenium+phantomjs alert()弹窗报错

    问题:用selenium+phantomjs 模拟登陆,网页用JavaScript的alert("登陆成功")弹出框,但是用switch_to_alert().accept()报错 ...

  9. selenium+python自动化93-Chrome报错:Python is likely shutting down

    遇到问题 报错信息:sys.meta_path is None, Python is likely shutting down 1.我的环境: python 3.6 selenium 2.53.6 c ...

随机推荐

  1. Codeforces Global Round 14 E. Phoenix and Computers

    题目链接 点我跳转 题目大意 给定 \(N\) 台电脑,起初每台电脑都是关闭的 现在你可以随意打开电脑,但如果第 \(i-1\).第 \(i+1\) 台电脑是开启的,则第 \(i\) 台电脑也会自动开 ...

  2. 01- Sublime的工具安装以及使用

    一 sublime安装与使用 sublime介绍: sublime是一个代码编辑器,可以编写HTML,PHP,js,css等文件. Sublime有哪些优点: 1.跨平台 2.扩展性强 3.提交小,运 ...

  3. 【MySQL】实现线上千万数据表添加字段操作以及缓存刷新

    需求背景: 由于业务需求,需要在线上用户表添加渠道字段,用于区分不同渠道注册的用户,目前该表有20+个字段,8个索引 线上用户数据大概1500W左右,需要不停机增加数据库字段,同时需要刷新Redis缓 ...

  4. 【布隆过滤器】基于Hutool库实现的布隆过滤器Demo

    布隆过滤器出现的背景: 如果想判断一个元素是不是在一个集合里,一般想到的是将集合中所有元素保存起来,然后通过比较确定.链表.树.散列表(又叫哈希表,Hash table)等等数据结构都是这种思路,存储 ...

  5. 洛谷P1085 不高兴的津津

    题目描述 津津上初中了.妈妈认为津津应该更加用功学习,所以津津除了上学之外,还要参加妈妈为她报名的各科复习班.另外每周妈妈还会送她去学习朗诵.舞蹈和钢琴.但是津津如果一天上课超过八个小时就会不高兴,而 ...

  6. hdu1530 最大团简单题目

    题意:       给你一个无向图,让你找到这个图里面的最大团是多少. 思路:       最大图案是NP问题,直接暴力搜索,如果当前的这个点可以加入当前最大团,那么就选择加入或者舍去,如果不能加入, ...

  7. Windows PE 第一章开发环境和基本工具使用

    第一章 Windows PE 基本工具 1.1开发语言MASM32 1.1.1设置开发环境 这个不细说了,我在整理Intel汇编的时候详细的说了环境搭建以及细节.地址是:http://blog.csd ...

  8. Day006 命令行传参

    命令行传参 有时候你希望运行一个程序时候再传递给它消息,这要靠传递命令行参数给main()函数实现. 使用方法 写测试代码. public static void main(String[] args ...

  9. K8s Scheduler 在调度 pod 过程中遗漏部分节点的问题排查

    问题现象 在TKE控制台上新建版本为v1.18.4(详细版本号 < v1.18.4-tke.5)的独立集群,其中,集群的节点信息如下: 有3个master node和1个worker node, ...

  10. [java] IDEA

    快捷键 shift+shift:搜索 ctrl+d:复制行 ctrl+x:删除行 ctrl+p:查看方法参数 ctrl+o:查看类方法 ctrl+h:查看类结构 ctrl+i:实现接口方法 ctrl+ ...