51Testing软件测试论坛

标题: 无法click的一个案例分享for selenium [打印本页]

作者: lsekfe    时间: 2022-9-29 14:25
标题: 无法click的一个案例分享for selenium
点击网页的第一个选择文件,如图:
[attach]143475[/attach]
示例代码
  1. <font size="3">  from selenium import webdriver  

  2.     

  3.    driver = webdriver.Chrome()  

  4.    driver.get('http://sahitest.com/demo/php/fileUpload.htm')  

  5.     

  6.    driver.find_element('id','file').click()</font>
复制代码
运行效果:报错了:invalid argument,无效的参数。
  1. <font size="3">D:\Python39\python.exe D:/pythonProject/deom1.py

  2.    Traceback (most recent call last):

  3.      File "D:\pythonProject\deom1.py", line 8, in <module>

  4.        driver.find_element('id','file').click()

  5.      File "D:\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 88, in click

  6.        self._execute(Command.CLICK_ELEMENT)

  7.      File "D:\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 396, in _execute

  8.        return self._parent.execute(command, params)

  9.      File "D:\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 435, in execute

  10.        self.error_handler.check_response(response)

  11.      File "D:\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response

  12.        raise exception_class(message, screen, stacktrace)

  13.    selenium.common.exceptions.InvalidArgumentException: Message: invalid argument

  14.      (Session info: chrome=104.0.5112.81)

  15.    Stacktrace:

  16.    Backtrace:

  17.        Ordinal0 [0x004F5FD3+2187219]

  18.        Ordinal0 [0x0048E6D1+1763025]

  19.        Ordinal0 [0x003A3D40+802112]

  20.        Ordinal0 [0x003C9171+954737]

  21.        Ordinal0 [0x003ECB8C+1100684]

  22.        Ordinal0 [0x003C8394+951188]

  23.        Ordinal0 [0x003ECDA4+1101220]

  24.        Ordinal0 [0x003FCFC2+1167298]

  25.        Ordinal0 [0x003EC9A6+1100198]

  26.        Ordinal0 [0x003C6F80+946048]

  27.        Ordinal0 [0x003C7E76+949878]

  28.        GetHandleVerifier [0x007990C2+2721218]

  29.        GetHandleVerifier [0x0078AAF0+2662384]

  30.        GetHandleVerifier [0x0058137A+526458]

  31.        GetHandleVerifier [0x00580416+522518]

  32.        Ordinal0 [0x00494EAB+1789611]

  33.        Ordinal0 [0x004997A8+1808296]

  34.        Ordinal0 [0x00499895+1808533]

  35.        Ordinal0 [0x004A26C1+1844929]

  36.        BaseThreadInitThunk [0x7635FA29+25]

  37.        RtlGetAppContainerNamedObjectPath [0x773A7A9E+286]

  38.        RtlGetAppContainerNamedObjectPath [0x773A7A6E+238]

  39.     

  40.     

  41.    进程已结束,退出代码为 1</font>
复制代码
解决方法
  ·用鼠标方法

  1. <font size="3"> from selenium import webdriver  

  2.    from selenium.webdriver import ActionChains  

  3.      

  4.    driver = webdriver.Chrome()  

  5.    driver.get('http://sahitest.com/demo/php/fileUpload.htm')  

  6.    ele_fie = driver.find_element('id','file')  

  7.    ActionChains(driver).click(ele_fie).perform()</font>
复制代码
说明
  ·不是很好解释
  · 提交了问题到stackoverflow:https://stackoverflow.com/questi ... onchains-works-fine,看看有无大神帮忙解释下。
  参考答案

  1. <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。





欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2