首页 > 解决方案 > 如何让 Selenium Python 单击按钮元素?

问题描述

所以我刚开始使用 Selenium for Python,我试图点击一个隐藏在几个 div 元素中的按钮元素;我已经尝试了很多东西,但没有任何效果。除了等待按钮可单击然后单击它的最后一部分之外,代码中的所有内容都有效。我将非常感谢这里的一些帮助,谢谢。:)

HTML:

html代码

代码试验:

我的python代码

错误堆栈跟踪:

我的python代码错误

标签: pythonseleniumxpathcss-selectorswebdriverwait

解决方案


点击**Maybe Later**按钮。Induce WebDriverWait() 和element_to_be_clickable() 并遵循 XPATH 或 CSS 选择器。

路径

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

推荐阅读