python - 如何使用 Selenium 和 Python 通过“onclick”找到并单击按钮?
问题描述
一个页面中有2个按钮,这2个按钮的区别是"onclick"
。
<button class="btn btn-primary" style="width: 96px;" type="button" id="YesBtn" onclick="check_security('security_div0')">OK</button>
<button class="btn btn-primary" style="width: 96px;" type="button" id="YesBtn" onclick="check_security('wlan1security_div0')">OK</button>
我在想使用xpath
:
driver.find_element_by_xpath("//form[@id='update-container-id']/div/div/div/div[2]/div/div[2]/table[1]/tbody/tr[1]/td[8]/div[3]/div/div/div/div[3]/button").click()
但它响应如下错误:
selenium.common.exceptions.ElementNotInteractableException: Message: Element <button id="YesBtn" class="btn btn-primary" type="button"> could not be scrolled into view
有谁可以帮我正确点击第二个按钮?非常感谢。
解决方案
尝试使用 x 路径//button[@onclick="check_security('wlan1security_div0')"]
driver.find_element_by_xpath("//button[@onclick=\"check_security('wlan1security_div0')\"]").click()
使用 Action 类,
button = driver.find_element_by_xpath("//button[@onclick=\"check_security('wlan1security_div0')\"]")
ActionChains(driver).move_to_element(button).click(button).perform()
使用 java 脚本执行器,
driver.execute_script("javascript:check_security('wlan1security_div0')")
推荐阅读
- python - MQTT 客户端仅在重新启动代理后连接
- dxf - 如何获取 DXF 文件中每个实体的图纸空间?
- javascript - 下一个.js。docker 镜像所需的文件
- java - DROOLS- 6.5 版与 java 8 的兼容性?
- javascript - switch case 直接到 end case
- javascript - 合并对象以获得数组中的平均值
- css - 如何在 JavaFX 中删除 TableView 标头中的边框
- git - .git/objects/pack 文件夹空间很高以及如何在从 Jenkins 克隆套件时啜饮这些文件
- laravel - 拉拉维尔。我应该把 CSS 文件放在哪里?
- python-3.x - 如何从 python、tkinter 中的 main 访问按钮单击?