pythonselenium-webdriverexpected-condition

How to make sure that a web element is completely loaded and ready with selenium expected_conditions


I am using selenium to retrieve data from a web page. I use expected_conditions selenium module in order to make the code more robust and to avoid having long sleeps in order to make sure that the data is loaded and can be used. Unfortunately I still got sporadic failures and I must use sleep() calls to get the data I want.

Is there an elegant, 100% sure solution to know if a web element is not only present and visible, but it is also "final"?

The following code shows a bit better what my problem is:

Instead of visibility, I have used also presence of the element but it was the same or a bit worse.

def check_id(driver):
    try:
        id_element = WebDriverWait(driver, 10).until(
            expected_conditions.visibility_of_element_located((By.ID, "whatever-id")))
        print(f"ID checkpoint before nap: {id_element.text} ")
        time.sleep(2)
        id_element = driver.find_element(By.ID, "whatever-id")
        print(f"ID checkpoint after nap: {id_element.text} ")
    except Exception:
        print("Exception")



Output: (on a sunny day)
ID checkpoint before nap: MY_EXPECTED_ID 
ID checkpoint after nap: MY_EXPECTED_ID 

        
Output: (on a rainy day)
ID checkpoint before nap:  
ID checkpoint after nap: MY_EXPECTED_ID 

Solution

  • EC.presence_of_element_located:

    An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.

    EC.visibility_of_element_located:

    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.

    EC.element_to_be_clickable:

    An Expectation for checking an element is visible and enabled such that you can click it.

    Suggestions: