Selenium click不生效 报错selenium.common.exceptions.InvalidArgumentException
记录在使用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有什么不同?
下面是我的一点小见解。
我大致看了一下源码,第一种方式是对远程的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的更多相关文章
- 【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 ...
- 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: ...
- selenium调用Firefox和Chrome需要注意的一些问题,和出现的报错selenium:expected [object undefined] undefined to be a string
在高版本selenium下如:selenium3.4.3 1.高版本的selenium需要浏览器安装一些补丁驱动 Firefox:geckodriver 下载网址:http://download.cs ...
- 解决selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid 'expiry'
解决selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid 'expiry' ...
- 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 ...
- python无法启动火狐浏览器且报错“selenium.common.exceptions.WebDriverException: Message: Unable to find a matching set of capabilities”
安装了python2,使用pip安装了selenium,但是在使用时,报了“selenium.common.exceptions.WebDriverException: Message: 'gecko ...
- python+selenium遇到元素定位不到的问题,顺便记录一下自己这次的错误(报错selenium.common.exceptions.NoSuchElementException)
今天在写selenium一个发送邮件脚本时,遇到一些没有找到页面元素的错误.经过自己反复调试,找原因百度,终于解决了.简单总结一下吧,原因有以下几点: 一:Frame控件嵌套,.Frame/Ifram ...
- python selenium+phantomjs alert()弹窗报错
问题:用selenium+phantomjs 模拟登陆,网页用JavaScript的alert("登陆成功")弹出框,但是用switch_to_alert().accept()报错 ...
- 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 ...
随机推荐
- kubespray续签k8s证书
查看证书过期时期 [root@node1 ~]# openssl x509 -in /etc/kubernetes/pki/apiserver.crt -noout -text |grep ' Not ...
- canvas性能优化总结
canvas的主要功能就是用来绘制内容,有时候为了给用户流畅的视觉感受,需要绘制的频率要求很高,这样对绘制的性能就有要求,那么怎么才能写出高性能的绘制代码呢. 尽可能少调用api 例如我们绘制一段线条 ...
- Relatives(容斥)
Relatives Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15708 Accepted: 7966 Descri ...
- 关于js中的回调函数callback,通俗易懂
前言 其实我一直很困惑关于js 中的callback,困惑的原因是,学习中这块看的资料少,但是平时又经常见,偶尔复制一下前人代码,功能实现了也就不再去追其原由,这么着,这个callback的概念就越来 ...
- 【Navicat】获取表结构的DDL语句以及获取更新表字段的操作的DDL
1.获取表结构的DDL语句 2.获取修改表结构某一字段的DDL语句 设计表-修改表字段(记住不要保存)-SQL预览
- hdu3724 字典树(商品条形码)
题意: 给你一堆商品的名字,然后给你一些条形码,问你这些条形码转换成的字符串的 前缀在商品中出现的个数,条形码的每个字母是八个二进制数字,有两种数,大的是小的2倍,小的是0,大的是1,这里面 ...
- Redis笔记整理
Redis 遵守BSD协议.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库.数据结构服务器. 特点: 1.Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时 ...
- layui中的分页laypage
1.html部分 <div id="test1"></div> 2.js部分: <script src="/static/build/lay ...
- Mongodb安装配置以及在thinkphp中的使用
1.Mongodb安装 Mongodb安装参照此文档: https://www.runoob.com/mongodb/mongodb-window-install.html php的mongodb扩展 ...
- ppt技巧一四步法调整PPT
声明:本文所有截图来源于网易云课堂--<和秋叶一起学PPT>,仅作为个人复习之用,特此声明! 常见配色方案 可以从模板或公司logo取色 图片的选择要高清.风格.主题一致