【问题标题】:Selenium wait for all class elementsSelenium 等待所有类元素
【发布时间】:2020-11-21 15:41:45
【问题描述】:

我正在尝试等待驱动程序等待,直到找到同一类的所有元素。

例如:

如果类是foo

我试试:

WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.CLASS_NAME, 'foo')))

我认为这只等待该类中第一个出现的元素。任何人都知道我怎样才能等到找到该类的所有元素。

【问题讨论】:

    标签: python selenium web-scraping webdriverwait expected-condition


    【解决方案1】:

    WebDriverWait 与 expected_conditions 结合为 presence_of_element_located() 将等待第一个匹配的 WebElement

    等到同一类的所有元素,例如foo class 存在,而不是 presence_of_element_located() 您需要为 presence_of_all_elements_located() 诱导 WebDriverWait 并且您的有效代码块将是:

    WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'foo')))
    

    注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

    【讨论】:

    • Selenium 如何在找到所有元素之前知道“所有”元素的位置?这个方法的注释是An expectation for checking that there is **at least one** element present on a web page
    • @poorguy 我认为你是对的。这个答案具有误导性,Selenium 文档也是如此。看Selenium源码,我觉得这两种方法没有区别。如果findElement(第一个实例)返回一些东西,则满足第一个,如果findElements(到目前为止找到的所有实例的列表)返回一些东西,则满足第二个。这使得它们在功能上有效地等效和冗余。这也意味着上面的答案是不正确的,除非 Selenium 可以预测未来,否则实际上它无法知道所有元素都已找到。
    猜你喜欢
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 2012-06-14
    • 2012-03-26
    • 1970-01-01
    • 2016-12-29
    • 2012-04-07
    相关资源
    最近更新 更多