python - 如何循环直到元素可点击
问题描述
我正在使用 python selenium chrome 驱动程序,我被困在这个问题上。
我怎样才能循环这段代码,直到其中一个元素是可点击的?
就像它最终可以点击一样,它应该被点击并打印(“可点击”),如果它仍然不可点击,它应该打印(“不可点击”)
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, "//BUTTON[@type='submit'[text()='Zum Warenkorb hinzufügen']"))).click()
WebDriverWait(driver, 150).until(EC.element_to_be_clickable((By.CLASS_NAME, "c-modal__content")))
解决方案
我不确定您对大写按钮的使用是否正确。使用与 html 中相同的语法。
还有一件事:用 text() 检查你的 xpath:它应该是://button[@type='submit' and text()='Zum Warenkorb hinzufügen']
此外,在一个元素的情况下,这种循环的一般情况是:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
wait = WebDriverWait(driver, 15)
while True:
try:
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@type='submit' and text()='Zum Warenkorb hinzufügen']")))
print("clickable")
element.click()
except TimeoutException:
break
推荐阅读
- python - 当一列单元格的值为零时,将另一列中的值设为零,将其下方的单元格设为零
- r - 多级负二项式回归的每个随机因子的 ICC
- c++ - VS2019:资源文件错误:中性(默认)(未知子语言:0x8)
- swift - 给定任何 x 值作为输入,如何获取 NSBezierPath 的 y 值
- java - Android中的嵌套网络视图
- python - 使用 TensorBoard 进行分析只会在“配置文件”选项卡中的 5 个可用工具中生成一个 (trace_viewer)
- regex - 正则表达式匹配贪婪、可选、非捕获组
- python - 如何修复 Python 中的 Module not found 错误?
- mysql - 考虑到数据更改的 mysql 查询的复杂旋转将在未来使用 Mysql 作为 DB 发生
- python - 为什么从 R 中的 stats 包中的函数 fft 返回的常量在评估时不返回信号的近似值?