[英]How to find and click the button by "onclick" with Selenium and Python?
There are 2 buttons in a page, and the difference between these 2 buttons is "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>
I was thinking to use xpath
:我正在考虑使用
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.
非常感谢。
try with the x-path //button[@onclick="check_security('wlan1security_div0')"]
尝试使用 x-path
//button[@onclick="check_security('wlan1security_div0')"]
driver.find_element_by_xpath("//button[@onclick=\"check_security('wlan1security_div0')\"]").click()
Using Action class,使用 Action 类,
button = driver.find_element_by_xpath("//button[@onclick=\"check_security('wlan1security_div0')\"]")
ActionChains(driver).move_to_element(button).click(button).perform()
using java script executor,使用java脚本执行器,
driver.execute_script("javascript:check_security('wlan1security_div0')")
As per the HTML you have provided, to click on the button using the onclick()
event you can use the following solution:根据您提供的HTML ,要使用
onclick()
事件单击按钮,您可以使用以下解决方案:
First Element(css_selector):第一个元素(css_selector):
driver.find_element_by_css_selector("button.btn.btn-primary#YesBtn[onclick*='security_div0']").click()
First Element(xpath):第一个元素(xpath):
driver.find_element_by_xpath("//button[@class='btn btn-primary' and @id='YesBtn'][@onclick=\\"check_security('security_div0')\\"]").click()
Second Element(css_selector):第二个元素(css_selector):
driver.find_element_by_css_selector("button.btn.btn-primary#YesBtn[onclick*='wlan1security_div0']").click()
Second Element(xpath):第二个元素(xpath):
driver.find_element_by_xpath("//button[@class='btn btn-primary' and @id='YesBtn'][@onclick=\\"check_security('wlan1security_div0')\\"]").click()
First and foremost, you are using a really long xpath which will be difficult to maintain.首先,您使用的是一个非常长的 xpath,这将很难维护。 You can narrow it down further.
您可以进一步缩小范围。
Now, some xpaths you can try:现在,您可以尝试一些 xpath:
1) Get the second button with the id YesBtn
(assuming there are only two buttons with that attribute) : 1) 获取 ID 为
YesBtn
的第二个按钮(假设只有两个按钮具有该属性):
driver.find_element_by_xpath("(//button[@id= 'YesBtn'])[2]");
2) Find by the onclick
attribute: 2)通过
onclick
属性查找:
driver.find_element_by_xpath("//button[@onclick= \'check_security(\'wlan1security_div0\')\']");
button1 with HTML :带HTML 的button1 :
<button class="btn btn-primary" style="width: 96px;" type="button" id="YesBtn" onclick="check_security('security_div0')">OK</button>
XPATH :路径:
//button[text()='OK' and @onclick="check_security('security_div0')"]
button2 with HTML :带有HTML 的button2 :
<button class="btn btn-primary" style="width: 96px;" type="button" id="YesBtn" onclick="check_security('wlan1security_div0')">OK</button>
HTML: HTML:
//button[text()='OK' and @onclick="check_security('wlan1security_div0')"]
try:
buttonVar = browser.find_element_by_css_selector(cssSel)
ActionChains(browser).move_to_element(buttonVar).click(buttonVar ).perform()
except Exception as ex:
print("button not found)
Firefox gives you several option you can use with selenium find_element or elements; Firefox 为您提供了几个选项,您可以使用 selenium find_element 或 elements;
I personally don't like xpath.我个人不喜欢 xpath。 They are sometimes long & unruly to work with but whatever suits your style.
他们有时很长而且不守规矩,但只要适合你的风格。 In my case css_selector works fine.
在我的情况下 css_selector 工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.