所以,我尝试使用selenium单击下一个按钮,我已尝试使用下面的代码,但结果是错误的。
元素
<input type="submit" name="submitNext" value="Next">
我的代码
driver.find_element_by_name("submitNext").click()
然后它会输出这些错误
Traceback (most recent call last):
File "C:/Users/thomas/PycharmProjects/test/mainapp/main.py", line 194, in
<module>
visa()
File "C:/Users/thomas/PycharmProjects/test/mainapp/main.py", line 174, in visa
driver.find_element_by_name("submitNext").click()
File "C:\Users\thomas\PycharmProjects\BudgetMain\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 487, in find_element_by_name
return self.find_element(by=By.NAME, value=name)
File "C:\Users\thomas\PycharmProjects\BudgetMain\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 955, in find_element
'value': value})['value']
File "C:\Users\thomas\PycharmProjects\BudgetMain\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "C:\Users\thomas\PycharmProjects\BudgetMain\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"submitNext"}
(Session info: chrome=66.0.3359.170)
(Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91),platform=Windows NT 10.0.16299 x86_64)
任何人都有任何想法如何点击该按钮没有错误?
答案 0 :(得分:3)
此错误消息......
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"submitNext"}
(Session info: chrome=66.0.3359.170)
(Driver info: chromedriver=2.36.540470)
...暗示 ChromeDriver 无法找到所需的元素。
根据您共享的HTML单击元素,您可以使用以下任一定位器策略:
css_selector
:
driver.find_element_by_css_selector("input[name='submitNext'][value='Next']").click()
xpath
:
driver.find_element_by_xpath("//input[@name='submitNext' and @value='Next']").click()
但是,您使用的二进制文件之间的主要问题是版本兼容性,如下所示:
支持 Chrome v63-65
支持 Chrome v65-67
因此 ChromeDriver v2.36 与 Chrome浏览器 v66.0 之间明显不匹配
@Test
。