seleniumappiumwebdriverwaitpython-appium

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.


Solution

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

    presence_of_element_located: Returns the WebElement if element is present on DOM and not even 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"))