记录在使用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. 【故障公告】数据库服务器再次 CPU 100% 引发全站故障

    今天五一劳动节的一大早 5:50-6:30 期间,我们使用的阿里云 RDS SQL Server 数据库实例再次出现 CPU 100% 问题,引发全站故障,由此给您带来麻烦,请您谅解. 我们发现故障后 ...

  2. 【cypress】5. 测试本地web应用

    在之前的cypress介绍里曾提到过,cypress虽然也可以测试部署好的应用,但是它最大的能力还是发挥在测试本地应用上. 本章主要内容就是关于如何测试本地web应用的概述: cypress与后台应用 ...

  3. hdu1146

    题意:       题意最蛋疼,其实是水题,这个题意整的我wa了一个多小时,我就改改题意吧..有一条河,刚开始的时候在左岸有一条船,这条船最多能运n个货物,一趟的时间是t,有m个货物,每个货物有自己到 ...

  4. XCTF-web_python_template_injection

    web_python_template_injection 这里涉及到flask的ssti漏洞(服务端模板注入). 简单点说就是,在使用flask/jinja2的模板渲染函数render_templa ...

  5. SpringBoot学习笔记:Spring Data Jpa的使用

    更多请关注公众号 Spring Data Jpa 简介 JPA JPA(Java Persistence API)意即Java持久化API,是Sun官方在JDK5.0后提出的Java持久化规范(JSR ...

  6. JavaScript 原始值与包装对象

    前言 随着 JavaScript 越来越流行,越来越多地开发者开始接触并使用 JavaScript. 同时我也发现,有不少开发者对于 JavaScript 最基本的原始值和包装对象都没有很清晰的理解. ...

  7. 自动化测试面试官:登录或注册时有验证码怎么处理?OCR图像识别技术大揭秘!

    本节大纲 读取cookie实现免登陆 pytesseract+tesseract-ocr实现图像识别 Pillow库对验证码截图 API接口实现图像识别 今天的这个技术点,为什么要给大家分享一下呢? ...

  8. 10.qml-组件、Loader、Component介绍

    1.组件介绍 一个组件通常由一个qml文件定义(单独文件定义组件), 实际也可以在qml里面通过Component对象来嵌入式定义组件 (4小节讲解). Component对象封装的内容默认不会显示, ...

  9. Zookeeper详细使用解析!分布式架构中的协调服务框架最佳选型实践

    Zookeeper概念 Zookeeper是分布式协调服务,用于管理大型主机,在分布式环境中协调和管理服务是很复杂的过程,Zookeeper通过简单的架构和API解决了这个问题 Zookeeper实现 ...

  10. 『动善时』JMeter基础 — 17、JMeter配置元件【HTTP请求默认值】

    目录 1.HTTP请求默认值介绍 2.HTTP请求默认值界面 3.HTTP请求默认值的使用 (1)用于演示的项目说明 (2)测试计划内包含的元件 (3)说明HTTP请求默认值用法 4.总结 5.拓展知 ...