DEV Community

Cover image for Selenium Python Exception Handling
Dilpreet Johal
Dilpreet Johal

Posted on

Selenium Python Exception Handling

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 –

code

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 –


📧 Subscribe to my mailing list to get access to more content like this

👍 Follow automationbro on Twitter for the latest updates

...

This post was written with the help of a high amount of caffeine. And, if this post helped you out and you would like to support my work, you can do that by clicking on the button below and continue supporting my caffeine love :)

Buy me a coffee

You can also support me by liking and sharing this content.

Thanks for reading!

Top comments (0)