TA的每日心情 | 擦汗 昨天 09:05 |
---|
签到天数: 1048 天 连续签到: 1 天 [LV.10]测试总司令
|
点击网页的第一个选择文件,如图:
示例代码
- <font size="3"> from selenium import webdriver
-
- driver = webdriver.Chrome()
- driver.get('http://sahitest.com/demo/php/fileUpload.htm')
-
- driver.find_element('id','file').click()</font>
复制代码 运行效果:报错了:invalid argument,无效的参数。
- <font size="3">D:\Python39\python.exe D:/pythonProject/deom1.py
- Traceback (most recent call last):
- File "D:\pythonProject\deom1.py", line 8, in <module>
- driver.find_element('id','file').click()
- File "D:\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 88, in click
- self._execute(Command.CLICK_ELEMENT)
- File "D:\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 396, in _execute
- return self._parent.execute(command, params)
- File "D:\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 435, in execute
- self.error_handler.check_response(response)
- File "D:\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
- raise exception_class(message, screen, stacktrace)
- selenium.common.exceptions.InvalidArgumentException: Message: invalid argument
- (Session info: chrome=104.0.5112.81)
- Stacktrace:
- Backtrace:
- Ordinal0 [0x004F5FD3+2187219]
- Ordinal0 [0x0048E6D1+1763025]
- Ordinal0 [0x003A3D40+802112]
- Ordinal0 [0x003C9171+954737]
- Ordinal0 [0x003ECB8C+1100684]
- Ordinal0 [0x003C8394+951188]
- Ordinal0 [0x003ECDA4+1101220]
- Ordinal0 [0x003FCFC2+1167298]
- Ordinal0 [0x003EC9A6+1100198]
- Ordinal0 [0x003C6F80+946048]
- Ordinal0 [0x003C7E76+949878]
- GetHandleVerifier [0x007990C2+2721218]
- GetHandleVerifier [0x0078AAF0+2662384]
- GetHandleVerifier [0x0058137A+526458]
- GetHandleVerifier [0x00580416+522518]
- Ordinal0 [0x00494EAB+1789611]
- Ordinal0 [0x004997A8+1808296]
- Ordinal0 [0x00499895+1808533]
- Ordinal0 [0x004A26C1+1844929]
- BaseThreadInitThunk [0x7635FA29+25]
- RtlGetAppContainerNamedObjectPath [0x773A7A9E+286]
- RtlGetAppContainerNamedObjectPath [0x773A7A6E+238]
-
-
- 进程已结束,退出代码为 1</font>
复制代码 解决方法
·用鼠标方法
- <font size="3"> from selenium import webdriver
- from selenium.webdriver import ActionChains
-
- driver = webdriver.Chrome()
- driver.get('http://sahitest.com/demo/php/fileUpload.htm')
- ele_fie = driver.find_element('id','file')
- ActionChains(driver).click(ele_fie).perform()</font>
复制代码 说明
·不是很好解释
· 提交了问题到stackoverflow:https://stackoverflow.com/questi ... onchains-works-fine,看看有无大神帮忙解释下。
参考答案
- <font size="3">In the DOM I see, the input element has attribute type=file, which means it is a file upload dialog that is related to OS and Selenium cannot perform operations on this one directly. Hence ActionChains.</font>
复制代码 翻译: 的确,在网页上,input标签是有这个属性的type=file,这是一个文件上传的对话框,跟OS相关,selenium无法对该元素直接执行操作,因此( )可以用ActionChains。
|
|