SDET Unicorns

Selenium Python Exception Handling

Exception Handling

Table of Content

In this post, we will take a look at how to do exception handling using Selenium Python and the SeleniumBase framework. So, before we see how we can do that let’s talk about why you need to handle exceptions in the first place, usually if there’s an error within your test you would want the test to fail and that is true for 95% of the scenarios.

But, you will run into certain scenarios where you will encounter an error and you would want to handle that differently instead of just throwing the error and failing your test.

For example –

  • If you are trying to find an element and if that element is not there, you want to perform some other action instead
  • Or, if you want to click a button but if that click doesn’t work you might want to perform some sort of retry functionality

Handling Exception using SeleniumBase

In the code below, we will try to find a product and if the product cannot be found then we will assert for the “not found” text instead –

from page_objects.shop_page import ShopPage
from selenium.common.exceptions import NoSuchElementException

class ShopTest(ShopPage):
    def test_search_products(self):
        # open page

        # search for product
        self.send_keys(self.search_input, "Toys")

        # assert product image
        except NoSuchElementException:
            self.assert_text("No products were found matching your selection.", self.no_products_txt)

When the exception was thrown after the product was not found, we handled it using the try/except block. Similarly, you can handle such exceptions for any of your use cases and perform some other action instead.

Check out the video below to learn more about handling exceptions using the SeleniumBase framework –

To learn more about Selenium Python, check out the full tutorial series here for free!

I hope this post helped you out, let me know if you have any questions in the comments below!

Happy testing! 

My Resources

Thrive Suite: The all-in-one WordPress theme I used to create my blog.
Jasper AI: My favorite AI writing tool.
Surfer SEO:  A tool to help you rank your content on the first page of Google.

Write Content Faster


Write blog posts that rank for affiliate terms!

Join our mailing list

Unlock the Secrets to Becoming a SDET Pro in the Industry!

Stay ahead of the curve! Join our mailing list for exclusive insights, tips, and the latest industry updates delivered straight to your inbox

Table of Content

Related Post

7 Principles of Software Testing

7 Principles of Software Testing with Examples

Software testing plays a crucial role in the software development life cycle as it helps ensure the quality, reliability, and performance of the final product. Software testing is not just about finding defects; it also provides valuable insights into the overall quality of the software. By conducting effective software testing, it shows how well the application behaves in different scenarios and whether it meets user expectations.

Read More »
Differences Between CI/CD and DevOps

Key Differences Between CI/CD and DevOps

Software development is far from a simple journey that involves merely executing code and launching applications. It often presents challenges, as it’s rare to get everything right from the start. Each time you implement new or frequent code changes, you inadvertently introduce potential issues that can be time-consuming to resolve. This complexity necessitates a structured approach to ensure smooth production.

Read More »