Toast Messages

Introduction

Toast Messages are temporary notification messages that appear on a webpage after a user performs an action. They are commonly used to inform users about successful operations, errors, warnings, or status updates without requiring additional interaction.

Modern web applications frequently display toast messages after login attempts, form submissions, payments, file uploads, and other user actions. Since toast messages usually appear for only a few seconds before disappearing automatically, proper synchronization is required while automating them using Selenium.

In Selenium, toast messages are typically handled using Explicit Wait together with visibility_of_element_located() to wait until the notification becomes visible before verifying its contents.

In this tutorial, you’ll learn how to handle Toast Messages using Selenium with Python, along with practical examples, real-world scenarios, common mistakes, and best practices.


What are Toast Messages?

Toast Messages are temporary notification messages displayed on a webpage to provide feedback to users after an action is performed.

Common examples include:

  • Login Successful

  • Payment Completed

  • File Uploaded Successfully

  • Invalid Credentials

  • Password Updated Successfully

  • Product Added to Cart

Toast messages are usually:

  • Dynamically generated.

  • Displayed for a short duration.

  • Automatically dismissed after a few seconds.

  • Loaded using JavaScript or AJAX requests.

Because of their temporary nature, synchronization plays an important role while automating them.


Why Automate Toast Messages?

Automating Toast Messages helps you:

  • Validate application behavior.

  • Verify success and error notifications.

  • Improve test coverage.

  • Handle dynamically loaded notifications.

  • Improve automation reliability.


Common Methods Used

MethodPurpose
WebDriverWait()Waits for dynamic conditions
visibility_of_element_located()Waits until the toast message becomes visible
find_element()Locates web elements
click()Performs user interactions
textRetrieves the displayed message
is_displayed()Verifies message visibility

Example

The following example clicks the notification trigger, waits for the toast message to become visible, and verifies that the notification contains some text.

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


# Topic: 27. Dynamic Web Elements - Toast Messages
# Practice site: https://the-internet.herokuapp.com/notification_message_rendered
# Run: pytest -s 27_examples/test_03_toast_messages.py
#
# Toast or flash messages appear briefly after an action. Click the trigger
# and wait for the message to become visible.


def test_toast_messages():
    driver = webdriver.Chrome()

    try:
        driver.get(
            "https://the-internet.herokuapp.com/notification_message_rendered"
        )

        driver.find_element(
            By.LINK_TEXT,
            "click here"
        ).click()

        flash_message = WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located(
                (
                    By.CSS_SELECTOR,
                    ".flash"
                )
            )
        )

        assert flash_message.text != ""

    finally:
        driver.quit()

Output

Action completed successfully and the notification message is displayed.

The toast message becomes visible successfully, and Selenium verifies that it contains text before continuing execution.

Note: The practice website displays different notification messages randomly. Therefore, the exact message may vary during different test executions. Instead of validating a fixed text value, the example verifies that the notification message is not empty.


Understanding the Code

Import the Required Classes

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

Imports:

  • webdriver for browser automation.

  • By for locating web elements.

  • WebDriverWait for synchronization.

  • Expected Conditions for waiting until the notification becomes visible.

Create the WebDriver

driver = webdriver.Chrome()

Launches a new Chrome browser session.

Open the Practice Website

driver.get(
    "https://the-internet.herokuapp.com/notification_message_rendered"
)

Opens the webpage that displays dynamic notification messages.

Click the Notification Trigger

driver.find_element(
    By.LINK_TEXT,
    "click here"
).click()

Clicking the link triggers the toast (flash) notification message.

Depending upon the application’s behavior, the notification may indicate:

  • Success

  • Failure

  • Warning

  • Informational messages

Wait for the Toast Message

flash_message = WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located(
        (
            By.CSS_SELECTOR,
            ".flash"
        )
    )
)

The WebDriverWait() method waits for a maximum of ten seconds until the toast message becomes visible.

The following Expected Condition is used:

visibility_of_element_located()

This condition repeatedly checks whether the notification message has appeared on the webpage.

As soon as it becomes visible, Selenium immediately proceeds without waiting for the full timeout period.

Verify the Message Content

assert flash_message.text != ""

This assertion verifies that the toast message contains some text.

The example intentionally avoids validating an exact message because the practice website generates different notifications randomly.

Close the Browser

driver.quit()

Closes the browser and terminates the WebDriver session.

This is a recommended practice to ensure that browser resources are released properly after test execution.


Handling Toast Messages

The most commonly used Expected Condition for toast messages is:

EC.visibility_of_element_located()

Example:

WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located(
        (
            By.CSS_SELECTOR,
            ".flash"
        )
    )
)

For applications where toast messages disappear quickly, Explicit Wait provides reliable synchronization without using unnecessary delays.


Verifying Toast Message Text

If the application’s behavior is predictable, you may verify the exact notification text.

Example:

assert "Login Successful" in flash_message.text

or

assert "Product Added Successfully" in flash_message.text

However, if notifications are generated dynamically or randomly, validating that the text is not empty is usually a better approach.


Practical Example

Suppose an e-commerce website displays the following notification after adding a product to the shopping cart:

Product Added Successfully

The automation script:

  • Adds a product to the cart.

  • Waits for the toast message to appear.

  • Verifies that the success notification is displayed.

  • Continues with the checkout process.

This validates both the application’s functionality and user feedback mechanisms.


Automation Testing Example

Consider an online banking application.

After transferring funds:

  • The transaction is processed.

  • A success notification appears.

  • The transaction reference number is generated.

The automation script:

  • Waits for the toast message.

  • Verifies that the transfer was successful.

  • Continues to download the transaction receipt.

Toast Messages are extremely common in enterprise-level applications that provide immediate user feedback.


Real-World Example

Toast Messages are commonly used in:

  • Banking applications

  • E-commerce websites

  • CRM systems

  • Healthcare portals

  • Airline booking systems

  • HR management systems

  • Enterprise web applications

They are particularly useful for displaying short-lived notifications without interrupting the user’s workflow.


Advantages of Automating Toast Messages

  • Validates success and error notifications.

  • Improves automation reliability.

  • Supports dynamic web applications.

  • Improves test coverage.

  • Handles temporary notifications efficiently.


Common Mistakes Beginners Make

Using time.sleep()

Many beginners write:

time.sleep(5)

This unnecessarily slows down automation scripts.

Instead, use:

WebDriverWait()

because Selenium immediately proceeds once the toast message becomes visible.

Waiting Too Long

Toast messages often disappear automatically after a few seconds.

Using unnecessarily long waits may cause the message to disappear before Selenium verifies it.

Always use reasonable timeout values.

Validating Exact Messages When They Are Dynamic

Some applications generate different notifications depending on business logic.

Instead of writing:

assert flash_message.text == "Operation Successful"

you may write:

assert flash_message.text != ""

when the application’s behavior is dynamic.

Using Fragile Locators

Always prefer stable locators such as:

  • ID

  • Name

  • CSS Selector

Avoid brittle XPath expressions whenever possible.


Best Practices

  • Use Explicit Wait for Toast Messages.

  • Prefer visibility_of_element_located() whenever possible.

  • Use stable locators for notification elements.

  • Validate the message contents appropriately.

  • Avoid unnecessary use of time.sleep().

  • Use reasonable timeout values for temporary notifications.

  • Synchronize properly before validating toast messages.


Conclusion

Toast Messages are temporary notifications that provide immediate feedback to users after an action is performed. Because they are displayed dynamically and often disappear automatically, proper synchronization is essential while automating them using Selenium. Using Explicit Wait together with visibility_of_element_located() provides a reliable and efficient approach for validating toast messages across modern web applications.


Frequently Asked Questions (FAQs)

Which Expected Condition is commonly used for Toast Messages?

The preferred condition is:

visibility_of_element_located()

Why shouldn’t I use time.sleep()?

time.sleep() always waits for the specified duration, whereas Explicit Wait immediately proceeds when the notification becomes visible.

Can Toast Messages disappear automatically?

Yes.

Most toast messages automatically disappear after a few seconds.

Are Toast Messages dynamically generated?

Yes.

Many applications generate toast messages dynamically using JavaScript, AJAX requests, or server-side responses.

Are Toast Messages commonly automated in Selenium?

Yes.

They are widely used across modern enterprise applications to validate user notifications and business workflows.


Key Takeaways

  • Toast Messages provide temporary feedback to users after an action is performed.

  • Use WebDriverWait() together with visibility_of_element_located() for reliable synchronization.

  • Avoid unnecessary use of time.sleep().

  • Validate notification text appropriately based on application behavior.

  • Prefer stable locators such as CSS Selector, ID, and Name.

  • Proper synchronization significantly improves automation reliability.

  • Toast Messages are widely used across modern enterprise web applications.