Ignoring Exceptions

Introduction

Ignoring Exceptions is one of the most useful capabilities provided by Fluent Wait in Selenium. While waiting for a condition to become true, Selenium may temporarily encounter exceptions because elements are not yet available on the webpage.

Instead of immediately failing the test, Fluent Wait allows Selenium to ignore specific exceptions and continue polling until:

  • The condition is satisfied, or

  • The timeout expires.

This makes Fluent Wait particularly useful for handling highly dynamic web applications where elements may temporarily be unavailable during page loading.

In this tutorial, you’ll learn what Ignoring Exceptions is, why it is used, its syntax, practical examples, real-world use cases, common mistakes, and best practices.


What is Ignoring Exceptions?

Ignoring Exceptions allows Selenium to temporarily ignore specified exceptions while waiting for an element or condition.

When an ignored exception occurs:

  • Selenium does not immediately fail the test.

  • Selenium waits for the specified polling interval.

  • Selenium checks the condition again.

  • Selenium continues polling until the timeout expires or the condition becomes true.

Common exceptions that may be ignored include:

  • NoSuchElementException

  • StaleElementReferenceException

  • ElementNotInteractableException (when appropriate)


Why Use Ignoring Exceptions?

Ignoring Exceptions helps you:

  • Handle highly dynamic web applications.

  • Prevent premature test failures.

  • Improve synchronization.

  • Reduce flaky automation tests.

  • Customize exception handling during waits.

  • Improve automation reliability.


Syntax

from selenium.common.exceptions import (
    NoSuchElementException
)

wait = WebDriverWait(
    driver,
    timeout=10,
    poll_frequency=0.5,
    ignored_exceptions=[
        NoSuchElementException
    ]
)

Where:

  • timeout=10 → Maximum wait time in seconds.

  • poll_frequency=0.5 → Selenium checks the condition every 0.5 seconds.

  • ignored_exceptions → Specifies which exceptions Selenium should ignore while waiting.

Note: Selenium ignores only the exceptions explicitly specified. Any other exceptions are immediately raised unless they are also included in ignored_exceptions.


Example

The following example demonstrates how Fluent Wait ignores temporary exceptions while waiting for dynamically loaded content.

After clicking the Start button, the Hello World! message is loaded asynchronously. During the waiting period, Selenium may temporarily encounter a NoSuchElementException because the element is not yet available.

Instead of immediately failing the test, Fluent Wait ignores the exception and continues polling every 0.5 seconds until the element becomes visible.

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


# Topic: 23. Fluent Wait - Ignoring Exceptions
# Practice site: https://the-internet.herokuapp.com/dynamic_loading/2
# Run: pytest -s 23_examples/test_03_ignoring_exceptions.py
#
# ignored_exceptions tells the wait to continue polling when specific
# exceptions occur instead of failing immediately.


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

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

        driver.find_element(
            By.CSS_SELECTOR,
            "#start button"
        ).click()

        wait = WebDriverWait(
            driver,
            timeout=10,
            poll_frequency=0.5,
            ignored_exceptions=[
                NoSuchElementException
            ],
        )

        heading = wait.until(
            EC.visibility_of_element_located(
                (
                    By.CSS_SELECTOR,
                    "#finish h4"
                )
            )
        )

        assert "Hello World!" in heading.text

    finally:
        driver.quit()

Output

Hello World!

The dynamically loaded element
became visible successfully.

Selenium ignored temporary
NoSuchElementException errors
while polling and immediately
continued execution once the
element became available.

Understanding the Code

Import NoSuchElementException

from selenium.common.exceptions import (
    NoSuchElementException
)

Imports the exception that Selenium will temporarily ignore while waiting.


Import WebDriverWait

from selenium.webdriver.support.ui import (
    WebDriverWait
)

Imports Selenium’s waiting class that supports customized polling intervals and exception handling.


Open the Practice Website

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

Opens Selenium’s Dynamic Loading practice page.

The webpage intentionally delays loading the Hello World! message, making it ideal for demonstrating ignored exceptions.


Click the Start Button

driver.find_element(
    By.CSS_SELECTOR,
    "#start button"
).click()

Begins the dynamic loading process.

At this point, the element does not immediately exist on the webpage.


Configure Fluent Wait

wait = WebDriverWait(
    driver,
    timeout=10,
    poll_frequency=0.5,
    ignored_exceptions=[
        NoSuchElementException
    ],
)

Creates a Fluent Wait with:

  • timeout=10 → Maximum wait time of 10 seconds.

  • poll_frequency=0.5 → Selenium checks the condition every 0.5 seconds.

  • ignored_exceptions=[NoSuchElementException] → Selenium ignores this exception during polling.

If Selenium cannot find the element immediately, it continues waiting instead of failing the test.


Wait Until the Element Becomes Visible

heading = wait.until(
    EC.visibility_of_element_located(
        (
            By.CSS_SELECTOR,
            "#finish h4"
        )
    )
)

Selenium repeatedly checks whether:

#finish h4

has become visible.

If the element is temporarily unavailable, Selenium ignores the NoSuchElementException and continues polling every 0.5 seconds.


Validate the Result

assert "Hello World!" in heading.text

Verifies that:

  • The dynamically loaded element became visible.

  • Fluent Wait successfully ignored temporary exceptions.

  • Selenium synchronized correctly with the webpage.


How Ignoring Exceptions Works

The following diagram illustrates the execution flow.

             Python Script
                    │
                    ▼
            Create Fluent Wait
                    │
                    ▼
              Check Condition
                    │
                    ▼
             Exception Occurred?
                 │          │
                No         Yes
                 │          │
                 ▼          ▼
          Condition      Ignore Exception
           Satisfied?            │
            │     │              ▼
           Yes    No        Wait 0.5 Seconds
            │     │              │
            ▼     ▼              ▼
       Continue  Continue    Check Again
                  Waiting         │
                                  ▼
                          Timeout Reached?
                              │        │
                             No       Yes
                              │        │
                              ▼        ▼
                       Continue Polling
                                    Throw
                             TimeoutException

Selenium continues polling until:

  • The element becomes available, or

  • The timeout expires.


Practical Example

Suppose an online shopping website loads product information dynamically after an API request.

During loading, Selenium may temporarily fail to locate the product card.

Instead of failing immediately, Fluent Wait ignores the temporary exception and continues polling until the product becomes available.

This significantly improves automation reliability.


Automation Testing Example

Consider an online banking application.

After submitting a transaction:

  • The server processes the request.

  • The dashboard updates dynamically.

  • The confirmation message appears after a short delay.

Using Ignoring Exceptions, Selenium continues waiting even if the confirmation message is temporarily unavailable during page updates.


Real-World Example

Ignoring Exceptions is commonly used in:

  • Banking applications.

  • E-commerce websites.

  • CRM systems.

  • Healthcare portals.

  • Airline booking systems.

  • Enterprise web applications.

It is particularly useful when application response times are unpredictable or elements are dynamically rendered.


Advantages of Ignoring Exceptions

  • Prevents premature test failures.

  • Improves synchronization.

  • Handles temporary element unavailability.

  • Reduces flaky automation tests.

  • Improves automation reliability.

  • Provides flexible exception handling.


Limitations

  • Incorrectly ignoring exceptions may hide genuine automation issues.

  • Requires understanding of Selenium exceptions.

  • Improper configurations may increase execution time.

  • Often unnecessary for simple synchronization scenarios.


Common Mistakes Beginners Make

Ignoring Too Many Exceptions

Avoid writing:

ignored_exceptions=[
    Exception
]

Ignoring every exception may hide genuine automation problems.

Always ignore only the exceptions expected during the waiting period.


Using Very Large Timeout Values

Avoid:

timeout=120

Large timeout values unnecessarily increase execution time when conditions are never satisfied.


Ignoring Exceptions Unnecessarily

Fluent Wait is an advanced synchronization technique.

For most automation scenarios, WebDriverWait with Expected Conditions is sufficient.

Use ignored exceptions only when temporary failures are expected.


Best Practices

  • Ignore only expected temporary exceptions.

  • Choose reasonable timeout and polling values.

  • Combine Ignoring Exceptions with appropriate Expected Conditions.

  • Avoid hiding genuine automation failures.

  • Prefer simpler synchronization techniques when customization is unnecessary.

  • Use Fluent Wait only when advanced exception handling is required.


Conclusion

Ignoring Exceptions is one of Fluent Wait’s most powerful capabilities. By allowing Selenium to temporarily ignore expected exceptions while polling, automation scripts become more reliable and better suited for highly dynamic web applications. Understanding when and how to ignore exceptions appropriately is essential for building stable and maintainable Selenium automation frameworks.


Frequently Asked Questions (FAQs)

What is Ignoring Exceptions in Selenium?

Ignoring Exceptions allows Fluent Wait to temporarily ignore specified exceptions while waiting for a condition to become true.

Which exceptions are commonly ignored?

Some commonly ignored exceptions include:

  • NoSuchElementException

  • StaleElementReferenceException

  • ElementNotInteractableException (when appropriate)

Can Fluent Wait ignore multiple exceptions?

Yes.

Multiple exceptions can be specified using the ignored_exceptions parameter.

Should I ignore all exceptions?

No.

Only ignore exceptions that are expected during the waiting period.

Is Ignoring Exceptions commonly used in real-world projects?

Yes.

It is particularly useful for highly dynamic applications where elements may temporarily be unavailable while the webpage is loading.


Key Takeaways

  • Ignoring Exceptions allows Fluent Wait to temporarily ignore specified exceptions during polling.

  • Selenium continues checking conditions until they become true or the timeout expires.

  • NoSuchElementException is one of the most commonly ignored exceptions.

  • Proper exception handling significantly improves synchronization and automation reliability.

  • Ignore only expected temporary exceptions to avoid hiding genuine automation failures.

  • Understanding Ignoring Exceptions is essential for building robust Selenium automation frameworks.