Selenium - wait until element is present, visible and interactable

前端 未结 4 2004
旧时难觅i
旧时难觅i 2020-11-22 01:41

I have a selenium script (python) that clicks a reply button to make the class anonemail appear. The time it takes for the class anonemail to appear varies. Because of that

相关标签:
4条回答
  • 2020-11-22 02:23

    After click reply button, use .visibility_of_element_located like bellow:

    browser.find_element_by_css_selector(".reply-button").click()
    
    #wait initialize, in seconds
    wait = WebDriverWait(browser, 10)
    
    email = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.anonemail'))).get_attribute("value")
    print(email)
    

    Following import:

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

    Waits docs

    0 讨论(0)
  • 2020-11-22 02:28

    You can use implicitly_wait

    from selenium import webdriver
    
    driver = webdriver.Firefox()
    driver.implicitly_wait(15)
    driver.get("http://url")
    driver.find_element_by_id("id_of_element").click()
    

    It waits until element is loaded.

    In your case implementation would be,

    browser.implicitly_wait(10)
    browser.find_element_by_css_selector(".reply-button").click()
    email=browser.find_element_by_css_selector(".anonemail").get_attribute("value")
    
    0 讨论(0)
  • 2020-11-22 02:33

    You can use waits. Check for more info on this link: selenium waits

    On the example bellow we are waiting 10 seconds for the element to be visible, using the function visibility_of_element_located.

    driver = webdriver.Firefox()
    driver.get("http://somedomain/url_that_delays_loading")
    try:
        element = WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located((By.ID, "myDynamicElement"))
        )
    finally:
        driver.quit()
    
    0 讨论(0)
  • 2020-11-22 02:37

    As per the best practices:

    • If your usecase is to validate the presence of any element you need to induce WebDriverWait setting the expected_conditions as presence_of_element_located() which is the expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible. So the effective line of code will be:

      WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".reply-button"))).click()
      
    • If your usecase is to extract any attribute of any element you need to induce WebDriverWait setting the expected_conditions as visibility_of_element_located(locator) which is an expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0. So in your usecase effectively the line of code will be:

      email = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "element_css"))).get_attribute("value")
      
    • If your usecase is to invoke click() on any element you need to induce WebDriverWait setting the expected_conditions as element_to_be_clickable() which is an expectation for for checking an element is visible and enabled such that you can click it. So in your usecase effectively the line of code will be:

      WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".reply-button"))).click()
      

    References

    You can find a couple of detailed discussion in:

    • WebDriverWait not working as expected
    • Selenium: Check for the presence of element
    0 讨论(0)
提交回复
热议问题