简体   繁体   English

如何让 Selenium Python 单击按钮元素?

[英]How do I make Selenium Python click on a button element?

So I just started using Selenium for Python and I am trying to click on a button element that is buried into a few div elements;所以我刚刚开始使用 Selenium for Python,我试图点击一个埋在几个 div 元素中的按钮元素; I've tried so many things but nothing works.我尝试了很多东西,但没有任何效果。 Everything in the code works besides the last part which is waiting for the button to be clickable and then clicking it.除了等待按钮可点击然后点击它的最后一部分之外,代码中的所有内容都有效。 I would greatly appreciate some help here, thanks.我将不胜感激这里的一些帮助,谢谢。 :) :)

HTML: HTML:

html代码

Code trials:代码试验:

我的蟒蛇代码

Error stacktrace:错误堆栈跟踪:

我的python代码错误

css selectors will become your best friend, css 选择器将成为你最好的朋友,

you should always look to add as many attributes as possible您应该始终希望添加尽可能多的属性

maybe_later_css = 'button[class="btn btn-danger"]'
# type str, '<tag-name>[<attribute-name> = <attribute-value>]'

driver.find_element_by_css_selector(maybe_later_css).click()

follow this format for all elements, its superior and works as expected every time所有元素都遵循这种格式,它的优越性和每次都按预期工作

the only complication is when there exists multiple buttons with them same class name, in which case you should find a different attribute to fill the [] brackets唯一的复杂之处是当存在多个具有相同类名的按钮时,在这种情况下,您应该找到不同的属性来填充 [] 括号

To click on **Maybe Later** button.单击**Maybe Later**按钮。 Induce WebDriverWait () and element_to_be_clickable () and following XPATH or CSS Selector.诱导WebDriverWait () 和element_to_be_clickable () 以及跟随 XPATH 或 CSS 选择器。

XPATH :路径

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//div[@class='modal-footer']//button[@Class='btn btn-danger x' and text()='Maybe Later']"))).click()

CSS Selector: CSS 选择器:

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div.modal-footer button.btn.btn-danger.x[style='danger']"))).click()

You need to import following libraries.您需要导入以下库。

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

The element with text as Maybe Later is within a Modal Dialog Box so to locate and click() on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :文本为“ Maybe Later ”的元素位于模态对话框中,因此要在元素上定位和click() ,您必须为element_to_be_clickable()引入WebDriverWait ,您可以使用以下任一定位器策略

  • Using CSS_SELECTOR :使用CSS_SELECTOR

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.modal-footer#eFooter button.btn.btn-danger.x[style='danger']"))).click()
  • Using XPATH :使用XPATH

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='modal-footer' and @id='eFooter']//button[@class='btn btn-danger x' and @style='danger']"))).click()
  • Note : You have to add the following imports :注意:您必须添加以下导入:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM