How to Handle Exception in Selenium Webdriver

Hello Welcome to Selenium Tutorial, today we are going to discuss how to Handle Exception in Selenium Webdriver without using much code.

In Selenium, we are going to face so many exceptions some of them are listed here.

Frequent Exceptions in Selenium

What is Exception?

An exception in Java in an event, which can disturb our program flow or you, can say it can terminate our program. An exception can come during execution of the program so we should follow better exception handling in our program/script.


Handle Exception in Selenium

 What is Exception handling?

Exception handling is a concept in the programming language to save our program from terminating. Let me explain in with the help of simple example

int c=5/0;

If we execute our program then at run time definitely, it is going to throw an exception called ArthematicException

Like this, we have the multiple exceptions in Selenium as well so let us see how we can handle this.


Handle exception in Selenium

In Java, we can handle exception using try- catch block.

Syntax: 1- try with 1 catch




Syntax 2- try with multiple catch block



Note- Make sure Parent Exception should come in last catch block otherwise we will face code, not reachable error.

Scenario for Selenium-

Consider a scenario in which we have to find an element with the help of XPath, id,name etc. and if Selenium is not able to find that element then it will throw NoSuchElementException and it will terminate your program, so in this we have to use try-catch block so that we can save our program being terminated.




We have the same exception in java.util also so make sure you have to import following package

 import org.openqa.selenium.NoSuchElementException;

For better automation practice always, try to use Exception handling.


Hope you like above post. Please comment if you finding any issue.

Please share with others as well. Have a nice day 🙂




  1. sudhr
  2. Sandeep
  3. sudhr
  4. Tom Prasad
  5. Mun
  6. Phani
  7. Pooja
  8. Poravikarthick