python+selenium,使用input控件进行文件上传时click操作报错:InvalidArgumentException

背景:使用AUTOIT方式进行文件上传时,选择文件按钮,直接进行click操作时报错,原代码如下:

from selenium import webdriver
import time
import os
wb = webdriver.Firefox()
wb.get('file:///D:/1.0%E6%B5%8B%E8%AF%95%E5%AD%A6%E4%B9%A0/6.0UI%E8%87%AA%E5%8A%A8%E5%8C%96/demo.html')
# 窗口最大化
wb.maximize_window()

f = wb.find_element_by_id('load')
f.click()
os.system(r'C:\Users\yu\Desktop\upfile.exe')

报错信息:如下

 1 Traceback (most recent call last):
 2   File "D:\python\python-demo\UI\Firefox.py", line 10, in <module>
 3     f.click()
 4   File "C:\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
 5     self._execute(Command.CLICK_ELEMENT)
 6   File "C:\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
 7     return self._parent.execute(command, params)
 8   File "C:\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
 9     self.error_handler.check_response(response)
10   File "C:\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
11     raise exception_class(message, screen, stacktrace)
12 selenium.common.exceptions.InvalidArgumentException: Message: Cannot click <input type=file> elements

解决方案:将click操作修改为鼠标事件,上传成功

action = ActionChains(wb)
action.move_to_element(f).click().perform()
action.release()
time.sleep(3)
os.system(r'C:\Users\yu\Desktop\upfile.exe')
time.sleep(3)

 

posted @ 2021-02-26 11:13  十月的雨  阅读(961)  评论(0编辑  收藏  举报