![](/img/trans.png)
[英]How to automate Selenium to click image button - find element by onclick? - Python
[英]How to find and click the button by "onclick" with Selenium and Python?
一个页面有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-path //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')")
根据您提供的HTML ,要使用onclick()
事件单击按钮,您可以使用以下解决方案:
第一个元素(css_selector):
driver.find_element_by_css_selector("button.btn.btn-primary#YesBtn[onclick*='security_div0']").click()
第一个元素(xpath):
driver.find_element_by_xpath("//button[@class='btn btn-primary' and @id='YesBtn'][@onclick=\\"check_security('security_div0')\\"]").click()
第二个元素(css_selector):
driver.find_element_by_css_selector("button.btn.btn-primary#YesBtn[onclick*='wlan1security_div0']").click()
第二个元素(xpath):
driver.find_element_by_xpath("//button[@class='btn btn-primary' and @id='YesBtn'][@onclick=\\"check_security('wlan1security_div0')\\"]").click()
首先,您使用的是一个非常长的 xpath,这将很难维护。 您可以进一步缩小范围。
现在,您可以尝试一些 xpath:
1) 获取 ID 为YesBtn
的第二个按钮(假设只有两个按钮具有该属性):
driver.find_element_by_xpath("(//button[@id= 'YesBtn'])[2]");
2)通过onclick
属性查找:
driver.find_element_by_xpath("//button[@onclick= \'check_security(\'wlan1security_div0\')\']");
带HTML 的button1 :
<button class="btn btn-primary" style="width: 96px;" type="button" id="YesBtn" onclick="check_security('security_div0')">OK</button>
路径:
//button[text()='OK' and @onclick="check_security('security_div0')"]
带有HTML 的button2 :
<button class="btn btn-primary" style="width: 96px;" type="button" id="YesBtn" onclick="check_security('wlan1security_div0')">OK</button>
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 为您提供了几个选项,您可以使用 selenium find_element 或 elements;
我个人不喜欢 xpath。 他们有时很长而且不守规矩,但只要适合你的风格。 在我的情况下 css_selector 工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.