Handling Exceptions Using Exception Handling Framework in Selenium Scripts – Selenium Tutorial #19

In last WebDriver tutorial we learned about 3 different types of important web elements like Web Tables, Frames and Dynamic elements and their handling mechanisms in selenium script

Before moving ahead with Framework tutorials in this Selenium training series, here in this tutorial we will learn about types of exceptions and how to handle exceptions in java and Selenium scripts. Developers/testers use exception handling framework to handle exception in selenium scripts.

Exception handling in Selenium 2

Example: When selenium script fails due to wrong locator, then developer should be able to understand the reason for failure and this can be achieved easily if the exception is handled properly in the program.

Below we have described the types of exceptions and the different ways how we can use exception handling framework in selenium scripts.

Exceptions are events due to which java program ends abruptly without giving expected output. Java provides a framework where user can handle exceptions.

There are three kinds of exceptions:

  1. Checked Exception
  2. Unchecked Exception
  3. Error

Class hierarchy of exception and error:

Exception handling in Selenium

#1) Checked Exception: Checked exception is handled during compile time and it gives compilation error if it is not caught and handled during compile time.

Example: FileNotFoundException, IOException etc.

#2) Unchecked Exception: In case of unchecked exception, compiler does not mandate to handle. Compiler ignores during compile time.

Example: ArrayIndexoutOfBoundException

#3) Error: When a scenario is fatal and program cannot recover then JVM throws an error. Errors cannot be handled by try catch block. Even if user tries to handle error by using Try catch block, it cannot recover from error.

Example: Assertion error, OutOfMemoryError etc.

Exception handling:

Try and Catch block:

try-catch blocks are generally used to handle exceptions. Type of exceptions is declared in catch block which is expected to come. When an exception comes in try block, immediately control moves to catch block.


try {
    br = new BufferedReader(new FileReader("Data"));
    } catch(IOException ie)

There can be multiple catch blocks for one try block depending upon type of exception.



try {
    br = new BufferedReader(new FileReader("Data"));
    } catch(IOException ie)
    } catch(FileNotFoundException file){

throws Exception:

throws keyword in java is used to throw an exception rather than handling it. All checked exceptions can be thrown by methods.


public static void main(String[] args) throws IOException
BufferedReader br=new BufferedReader(new FileReader("Data"));
     while ((line = br.readLine()) != null)

finally block:

finally block executes irrespective of execution of try catch block and it executes immediately after try/catch block completes.

Basically file close, database connection etc. can be closed in finally block.


try {
    br = new BufferedReader(new FileReader("Data"));
    } catch(IOException ie)
Finally {

In the above example, BufferReader stream is closed in finally block. br.close() will always execute irrespective of execution of try and catch block.

Note: finally block can exist without any catch block. It is not necessary to have a catch block always.

There can be many catch blocks but only one finally block can be used.

Throwable: Throwable is parent class for error and exception. Generally it is difficult to handle errors in java. If programmer is not sure about the type of error and exception, then it is advised to use Throwable class which can catch both error and exception.


try {
   br = new BufferedReader(new FileReader("Data"));
     } catch (Throwable t)

Exceptions in Selenium WebDriver:

Selenium has its own set of exceptions. While developing selenium scripts, programmer has to handle or throw those exceptions. Below are few examples of exceptions in selenium.


ElementNotVisibleException: If selenium tries to find an element but element is not visible within page

NoAlertPresentException: If user tries to handle an alert box but alert is not present.

NoSuchAttributeException: While trying to get attribute value but attribute is not available in DOM.

NoSuchElementException: This exception is due to accessing an element which is not available in the page.

WebDriverException: Exception comes when code is unable to initialize WebDriver.


Exception handling is the essential part of every java program as well as selenium script. We can build robust and optimal code by handling exception in smart ways. And it is also a best practice to handle exceptions in script which will give you a better report when program fails due to any reason.

Here we have tried to cover the process and framework of exception handling which is required to be implemented in selenium scripts.

Remember it is not mandatory to always handle exception in try-catch block. You can also throw an exception depending upon the requirement in script.

Next Tutorial #20: In the upcoming tutorial, we would discuss about the various types of testing frameworks available. We would also study about the pros and cons of using a fledged framework approach in automation testing. We would discuss in detail about Test data driven framework.

Please post your queries, related to handling exception in Selenium WebDriver, if you have any.


#1 Ahmed

Can we have a session on fitnesse+selenium+Eclipse . Please

#2 Keyur Makwana

In try and catch block if error occurred then it will be handle in catch block but what about happen if we can used throws ?

#3 Supriya Kamat

Hi very useful.
Can you share some simple usage examples of sleneium exceptions mentioned in the end?

#4 Amaresh

Hi Keyur,throws will be used when you dont want to handle any error.Lets say you are you want to find an element in webpage but the element is not shown for some reason.In this case you may not want your code to fail because element is not available.So in this case you can catch the exception and navigate again to the elemnet.If you want to fail the testcase in first time if you dont find the element then use throwd.It will come out of the programme.

#5 Amaresh

Supriya.Hope this example solves your doubt.
public class elementNotfoundExample {

public static void main(String[] args) throws InterruptedException {

WebDriver driver=new FirefoxDriver();
}catch(ElementNotFoundException e)


#6 Sumit


I am currently working on a project in which I have created scripts using Web Driver & TestNG Reporting.

I have put the code for all the main test scripts under try…catch block to handle exceptions. After running the test suite I am able to successfully catch the exceptions in eclipse console. But the problem I am facing is “Results of running suite” (TestNG) tab in eclipse shows all the scrips as passed.

I am looking for such a mechanism so that if any exception occurs while executing the test suite it should mark the corresponding test/method as failed.

Please suggest.

#7 Prasanthi

am using the below files

iedriver-2.46-32bit—-browser version ie-11
chromedriver-2.16——browser version 44.0
standalone server-2.46.0

i am getting the below issues

–>just i able to login my application after then i need to click on element by using mousehover actions
but its not executing properly.
–>if i execute multiple times mouse hover action is not executing in chrome browser
–>In ie browser its executing instead of one element its performing action on another element.

how can i resolve these issues

can anyone suggest me please…

Thanks & Regards,

#8 vidya

Can we automate deskdop applications using selenium web driver.

#9 jayaram

This is useful context, but it would be nice if you mention differences between Throws,Throw,Throwable classes

#10 ninhanh8x

Useful context. Thanks.

#11 ItsMe

To answer your question Vidya, no Selenium cannot be used to automate desktop applications. For that you could look at AutoHotkey which is free. There are other utilities such as AutoIt, but I don’t know if they are free or not.

#12 booathi

hi vidya,prasanthi i need to learn selnium pls help

#13 raji

i hate selenium

#14 ragupathi

i want to learn pls help

Leave a Comment