如何使 Selenium Python 单击按钮元素?
How do I make Selenium Python click on a button element?
所以我刚开始将 Selenium 用于 Python,我试图点击一个隐藏在几个 div 元素中的按钮元素;我尝试了很多东西,但没有任何效果。除了等待按钮可单击然后单击它的最后一部分之外,代码中的所有内容都有效。我将不胜感激这里的一些帮助,谢谢。 :)
HTML:
代码试验:
错误堆栈跟踪:
css 选择器将成为您最好的朋友,
您应该始终注意添加尽可能多的属性
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()
对所有元素都遵循这种格式,它的性能优越并且每次都按预期工作
唯一的问题是存在多个具有相同 class 名称的按钮,在这种情况下,您应该找到不同的属性来填充 [] 括号
点击**Maybe Later**
按钮。
引入 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 选择器:
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div.modal-footer button.btn.btn-danger.x[style='danger']"))).click()
您需要导入以下库。
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
文本为 Maybe Later 的元素在 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 :
内
使用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()
使用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()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
所以我刚开始将 Selenium 用于 Python,我试图点击一个隐藏在几个 div 元素中的按钮元素;我尝试了很多东西,但没有任何效果。除了等待按钮可单击然后单击它的最后一部分之外,代码中的所有内容都有效。我将不胜感激这里的一些帮助,谢谢。 :)
HTML:
代码试验:
错误堆栈跟踪:
css 选择器将成为您最好的朋友,
您应该始终注意添加尽可能多的属性
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()
对所有元素都遵循这种格式,它的性能优越并且每次都按预期工作
唯一的问题是存在多个具有相同 class 名称的按钮,在这种情况下,您应该找到不同的属性来填充 [] 括号
点击**Maybe Later**
按钮。
引入 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 选择器:
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div.modal-footer button.btn.btn-danger.x[style='danger']"))).click()
您需要导入以下库。
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
文本为 Maybe Later 的元素在 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
使用
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()
使用
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()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC