【问题标题】:How to loop until element clickable如何循环直到元素可点击
【发布时间】:2021-07-03 10:38:51
【问题描述】:

我正在使用 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")))

【问题讨论】:

  • 澄清您的问题。当这两个元素变为可点击时,您是否需要点击它?
  • 哦,抱歉,做到了

标签: python selenium google-chrome selenium-chromedriver python-webbrowser


【解决方案1】:

我不确定您对大写按钮的使用是否正确。使用与 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

【讨论】:

  • 我得到这个错误:除了 TimeoutException: NameError: name 'TimeoutException' is not defined
  • 添加 from selenium.common.exceptions import TimeoutException
猜你喜欢
  • 2021-05-17
  • 2019-11-01
  • 2021-12-26
  • 2022-01-08
  • 1970-01-01
  • 2019-08-04
  • 2016-12-19
  • 2016-06-08
  • 2017-04-16
相关资源
最近更新 更多