FAQ selenium无法click的一个案例分享

发表于:2022-9-29 09:44

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:老吴的博客    来源:博客园

  点击网页的第一个选择文件,如图:
  示例代码
   from selenium import webdriver  
    
   driver = webdriver.Chrome()  
   driver.get('http://sahitest.com/demo/php/fileUpload.htm')  
    
   driver.find_element('id','file').click()
  运行效果:报错了:invalid argument,无效的参数。
   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
  解决方法
  ·用鼠标方法
   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()
  说明
  ·不是很好解释
  · 提交了问题到stackoverflow:https://stackoverflow.com/questions/73356693/why-click-method-didnt-take-effect-but-actionchains-works-fine,看看有无大神帮忙解释下。
  参考答案
  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.
  翻译: 的确,在网页上,input标签是有这个属性的type=file,这是一个文件上传的对话框,跟OS相关,selenium无法对该元素直接执行操作,因此( )可以用ActionChains。
  本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号