Dummie1138
Dummie1138

Reputation: 81

WebDriverWait.until.expected_conditions.presence_of_element_located not waiting for reloaded DOM

I have an app with 2 buttons. Button A takes the app to a new page with no buttons, then returns to the page with 2 buttons. My automated test is meant to click on Button A, wait while the app heads to the new pages and returns, then click on Button B.

The code:

    el05a = WebDriverWait(driver, 120).until(
        expected_conditions.presence_of_element_located((By.ID, "id_of_button_a"))
    )
    el05a.click()
    el05b = WebDriverWait(driver, 120).until(
        expected_conditions.presence_of_element_located((By.ID, "id_of_button_b"))
    )
    el05b.click()

However, I receive a StaleElementReferenceException about button B not being in DOM anymore.

Obviously, button B is not gonna be in the DOM while the app is at the new page, but why does my code not know to wait until the presence of button B is located? I thought presence_of_element_located means the code would be on hold until the element is located.

I know this could "technically" be patched with a time.sleep module but I'm trying to avoid that.

Upvotes: 2

Views: 343

Answers (2)

Nandan A
Nandan A

Reputation: 2922

visibility_of_element_located: Returns the WebElement once it is located and visible.

  • 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.

presence_of_element_located: Returns the WebElement if element is present on DOM and not even visible.

  • 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.

Please change it from

expected_conditions.presence_of_element_located((By.ID, "id_of_button_a"))

to

expected_conditions.visibility_of_element_located((By.ID, "id_of_button_a"))

Upvotes: 1

Abhishek Dhoundiyal
Abhishek Dhoundiyal

Reputation: 1417

As per your query it seems likes as your checking presence_of_element_located and which only check for it presence and not the visibility of the element.

Try replacing the presence_of_element_located with visibility_of_element_located.


There is difference between visibility_of_element_located and presence_of_element_located.

1) visibility_of_element_located

Checking that an element is present on the DOM of a page and visible. Basically it tests if the element we are looking for is present as well as visible on the page.

2) presence_of_element_located

Checking that an element is present on the DOM of a page. Basically it tests if the element we are looking for is present somewhere on the page.

Code:

 el05a = WebDriverWait(driver, 120).until(
        expected_conditions. visibility_of_element_located((By.ID, "id_of_button_a"))
    )
    el05a.click()
    el05b = WebDriverWait(driver, 120).until(
        expected_conditions. visibility_of_element_located((By.ID, "id_of_button_b"))
    )
    el05b.click()

Upvotes: 2

Related Questions