Search code examples
pythonseleniumclickwaittoast

Python Selenium wait until toast message disappears


I have a page where after clicking a button the page starts loading and after a few seconds a toast message appears. After either the pager-loader or the toast message disappears I want to click on another button which takes me to another page.

Before the toast message appears there is a page loader for few seconds ("ajax-disable request-loader...") with the following code:

<div class="footer">
    <div class="footer-inner">
         <small>Loading time: 0.91898512840271 / Memory usage: 2.77&nbsp;MB</small>
    </div>
    <div class="footer-tools">
        <span class="go-top">
            <i class="fa fa-angle-up"></i>
        </span>
    </div>
</div>
<!-- END FOOTER -->
<!-- END BODY -->
<div class="ajax-disable request-loader-AX3d868Qh30tNqeQ5KtqqFF" style="position: absolute; left: 20px; top: 137.535px; width: 876px; height: 2850px;"></div>
</body>
</html>

And after that the div with the page loader disappears and on the same place the code for toast message appears which is:

<!-- END FOOTER -->
<!-- END BODY -->
<div id="toast-container" class="toast-top-right">...</div>
</body>
</html>

And then it also disappears.

My code is the following:

WebDriverWait(driver, 15).until(EC.invisibility_of_element_located((By.ID, "toast-container")))
driver.find_element_by_id("back").click()

I tried this and it works with no error messages but whatever text I put in the ID ((By.ID, "random123"))) even a random one it still works. And I guess it isn't how it is supposed to work.

I also tried until the message appears but then I get the print("toast message not found").

    try:
        WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "toast-container")))
    except TimeoutException:
        print("toast message not found")

Any idea what is the proper way? Thanks.


Solution

  • If you know the toast ID so the correct way is what you did.

    wait = WebDriverWait(driver, 15)
    wait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR, "[class^='request-loader']")))
    wait.until(EC.invisibility_of_element_located((By.ID, "toast-container")))
    driver.find_element_by_id("back").click()
    

    I tried this and it works with no error messages but whatever text I put in the ID ((By.ID, "random123")) even a random one it still works. And I guess it isn't how it is supposed to work.

    This is exactly the way it works! we verify there is no element with ID as random!

    Note: I added a wait for the loader to disappear.