简体   繁体   English

如何使用Selenium和Python通过“onclick”查找并单击按钮?

[英]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;

  1. by pressing f12 to bring up the inspector,按 f12 调出检查器,
  2. use the "pick an item" icon in the left corner to select the item you want to view.使用左上角的“选择项目”图标选择您要查看的项目。
  3. This will highlight the text for that item in the inspector, which you can then right click on to bring up a menu where you select "copy" where there are at least 2 find_element options, css_selecor & xpath.这将在检查器中突出显示该项目的文本,然后您可以右键单击该文本以显示一个菜单,您可以在其中选择“复制”,其中至少有 2 个 find_element 选项、css_selecor 和 xpath。

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.

相关问题 如何自动化 Selenium 以单击图像按钮 - 通过 onclick 查找元素? - Python - How to automate Selenium to click image button - find element by onclick? - Python 用硒和Python单击“ onclick”按钮 - Click 'onclick' button with selenium and Python 如何找到使用python在Selenium中单击的按钮? - how to find button to click in Selenium using python? 如何在 Python selenium 中找到要单击按钮的元素? - How to find element to click button in Python selenium? Python + Selenium —无法单击按钮(onClick = openWIndow) - Python + Selenium — unable to click button (onClick=openWIndow) 如何使用硒单击/查找按钮 - How to click/find button with selenium 如何使用 Selenium 和 Python 按名称查找并单击页面中的按钮 - How to find and click on a button in page by name using Selenium and Python 如何在含硒的网站上找到并单击按钮? (Python 2.7-Debian) - How to find and click on a button on a website with selenium? (Python 2.7 - Debian) 如何通过带有 Python Selenium 的 aria-label 找到并单击 JavaScript 按钮? - How to find and click a JavaScript button by aria-label with Python Selenium? Python + Selenium:如何单击“ onclick”元素? - Python + Selenium: How can click on “onclick” elements?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM