python - 如何让 Selenium Python 单击按钮元素?
问题描述
所以我刚开始使用 Selenium for Python,我试图点击一个隐藏在几个 div 元素中的按钮元素;我已经尝试了很多东西,但没有任何效果。除了等待按钮可单击然后单击它的最后一部分之外,代码中的所有内容都有效。我将非常感谢这里的一些帮助,谢谢。:)
HTML:
代码试验:
错误堆栈跟踪:
解决方案
点击**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
推荐阅读
- python - python虚拟环境中的默认PIP是古老的,如何修复它?
- apache - Apache OpenID Connect 提前刷新令牌
- c++ - 为什么写空的 std::istringstream.rdbuf() 会设置失败位?
- javascript - 在渲染函数 React Redux 中未使用属性时防止组件重新渲染
- javascript - src dataId 值到 dest newDataId 它没有复制
- java - 如何改进抓取算法的图像分割?
- c++ - 了解 Google 基准测试结果
- r - 将因子转换为 R 中的仅时间
- python - 如何根据 Plotly 中数据框中的另一列为标记着色?
- java - 使用并行性逐行处理文本文件,但保留顺序