如何通过"onclick"找到并点击按钮与Selenium和Python? [英] How to find and click the button by "onclick" with Selenium and Python?

查看:189
本文介绍了如何通过"onclick"找到并点击按钮与Selenium和Python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

页面中有2个按钮,这两个按钮之间的区别是"onclick".

There are 2 buttons in a page, and the difference between these 2 buttons is "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()

但是它会响应以下错误:

But it responses the error as below:

selenium.common.exceptions.ElementNotInteractableException: Message: Element <button id="YesBtn" class="btn btn-primary" type="button"> could not be scrolled into view

有人可以帮助我正确单击第二个按钮吗?非常感谢.

Does anyone can help me to click the 2nd button correctly? Thanks a lot.

推荐答案

尝试使用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脚本执行程序,

using java script executor,

driver.execute_script("javascript:check_security('wlan1security_div0')")

这篇关于如何通过"onclick"找到并点击按钮与Selenium和Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆