【问题标题】:How to wait element of a list is detroyed?如何等待列表的元素被破坏?
【发布时间】:2021-04-09 18:23:57
【问题描述】:

我想单击表格中删除其行的所有按钮:

    try:
        elements = driver.find_elements(By.XPATH, '//table//button[@class="deleteEntry"]')
    except NoSuchElementException:
        print("      [NoSuch] Buttons not found")
        return False

    for element in elements:        
        element.click()
        # How to wait element is destroyed ?

我必须一一点击它们,然后一一等待它们被摧毁。 我知道找到并点击它们,但我如何等待它们被破坏?

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    我假设在您单击“删除”按钮后,某些元素会消失(不可见或从 DOM 中删除)。

    您可以尝试显式等待。

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    # get the element here
    # elem_to_disappear = ...
    
    # click delete button
    del_btn.click()
    
    # wait until the element is gone
    WebDriverWait(driver, 10).until(
        EC.invisibility_of_element(elem_to_disappear)
    )
    
    

    这里有更多文档:https://www.selenium.dev/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html#selenium.webdriver.support.expected_conditions.invisibility_of_element

    【讨论】:

    • 检查我在使用invisibility_of_element_located 和隐式等待stackoverflow.com/questions/65373501/… 时遇到的问题 如果您使用invisibility_of_elementinvisibility_of_element_located,最好避免使用隐式等待
    【解决方案2】:

    尝试driver.implicitly_wait(10) 隐式等待告诉 WebDriver 在尝试查找任何不能立即可用的元素(或多个元素)时轮询 DOM 一段时间。如果它不起作用,请尝试来自时间库的 time.sleep(waiting_time)

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 2021-09-08
      • 2016-02-29
      • 1970-01-01
      • 2016-03-09
      • 2015-02-14
      • 1970-01-01
      • 2015-12-05
      • 2021-10-28
      相关资源
      最近更新 更多