记录在使用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. 大数翻倍法求解CRT

    目录 正文 引入 大数翻倍法 复杂度证明 大数翻倍法的优势 最后的最后:上代码! 注:做法和思路是 zhx 在一次讲课中提出的,如有侵权,请联系作者删除 其实别的题解也有提到过暴力做法,但这里将会给出 ...

  2. LA3266田忌赛马

    题意:       田忌和齐王赛马,两个人每人n匹马,每个马都有自己的速度,赢一场得到200分,输一场失去200分,平则不得分,问田忌可能得到的最高得分是多少? 思路:       又是一个比较经典的 ...

  3. 两种常见的Vlan间通信的方式

    目录 一:三层交换机方式 二:单臂路由方式 一:三层交换机方式 如图,PC1和PC2是企业内网的主机,属于不同的部门,故属于不同的VLAN.在交换机上配置vlan 10和vlan 20,并且配上主机的 ...

  4. android安全学习、工具库、框架

    在介绍android工具之前,先理清android中出现的文件格式: java:android源码 class:java编译后生成: dex: 由dx工具编译class而成,由dalvik执行: sm ...

  5. 缓冲区溢出之栈溢出利用(手动编写无 payload 的 Exploit)

    0x01 介绍 Exploit 的英文意思就是利用,它在黑客眼里就是漏洞利用.有漏洞不一定就有Exploit(利用),有Exploit就肯定有漏洞.编写缓冲区溢出的Exploit分为3个方面:漏洞溢出 ...

  6. 手脱UPX3.91壳(练习)

    0x01 准备 OD UPX加壳程序 可以加壳的软件 0x02 给软件加壳 我找了半天发现winhex不错,而且是没壳的可以直接加壳 1.复制一份可执行文件 将赋值好的文件用UPX3.91加壳 0x0 ...

  7. PhotoShop 第一课 功能认识

    功能认识 1.基本界面 可以对各工具栏进行编辑,对工具/栏目进行勾选添加和整合并搭建自己的专属操作页面. 2.画布设置 拍照或者画画都需要一个东西来呈现这个东西叫做画布(可以通过导航栏-文件-新建画布 ...

  8. mongoDB常用

    登陆{ 本地的话直接mongo,如果是docker直接就这样docker exec -it 2d71a13e3128 mongo 或者直接这样 mongo 127.0.0.1:27017 } 退出是 ...

  9. [CTF]盲文对照表

    [CTF]盲文对照表 摘自:https://wenku.baidu.com/view/28b04fd380eb6294dd886ca7.html 学点盲文 盲文又称点字,国际通用的点字由6个凸起的圆点 ...

  10. java.lang.ClassNotFoundException的解决方案

    举一个特定的例子 java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource 到Maven中央仓库下载 当我们看 ...