Exceptions & How to Handle Exceptions in Selenium WebDriver

Reading Time: 3 minutes

Hello Readers,
In this blog, we will learn about Exceptions, how to handle exceptions in Selenium WebDriver, and common exceptions in selenium. Let’s Start.

What is an Exception?

Exception as the name suggests is an event that happens at the time of execution of a program. There may be several reasons behind the occurrence of exceptions that indicate the halt in the program flow.
Exceptions are classified into two types:

  • Checked Exception: It is a type of exception that must be either caught or declared in the method in which it is thrown.
  • Unchecked Exception: This exception occurs at the time of execution and also known as runtime exceptions.

Handling Selenium exception:

Exception handling is a way that detects and resolves runtime exceptions and communication errors in selenium. Ways to solve different exceptions in Selenium WebDriver.

  • Try-catch: Try-catch statement consists of a try keyword/block followed by one or more catch clauses. Try command indicates the start of the block & catch places at the end of the try block which helps to resolve the exceptions.

//Write your code here
} catch (Exception e){
//code to handle the exception

  • Throws: We use Throws keyword to throw an exception rather than handling it.

public void main function_name() throws Exception{
} catch(Exception e){
// Do whatever you wish to do here
// Now throw the exception back to the system

  • Multiple Exceptions: A programmer can declare various exceptions in the throw clause.

public void main function_name() throws ExceptionType1, ExceptionType2{
} catch(Exceptiontype1 e){
// code to handle the ExceptionType1
} catch(Exceptiontype2 e){
// code to handle the ExceptionType2

  • Finally: A finally block contains all the crucial statements that executes whether an exceptions occur or not. It executes immediately after the try-catch block completes.

}catch(ExceptionType1 e){
//catch block
} catch(ExceptionType2 e){
//catch block
} finally{
//Finally block always executes

Common Exceptions in Selenium WebDriver:

When developing selenium scripts, a programmer has to handle or throw different exceptions. Some of the exceptions that occur most in selenium scripts are as follows:


This occurs when WebDriver is not able to find and locate elements. Usually, this happens when testers write incorrect element locators in the findElement(By) method.

For example: Suppose the correct id for the text field was ‘first_field’ but the tester incorrectly mentioned it as “submit”. In this case, WebDriver cannot locate the element and NoSuchElementException will be there.

}catch(NoSuchElementException e){

In this case, the exception occurs even if the element is not present.


This exception arises when WebDriver tries to switch to an invalid window. The code below can throw NoSuchWindowException if the window handle doesn’t exist or is not available to switch.
The below code can throw NoSuchWindowException if the window handle doesn’t exist or is not available to switch.



This exception occurs when the locators(id/xpath/css selector) we have provided in the selenium program code are trying to find the web element which is hidden from displaying on the webpage.

For example:
In the code below, if the type of button with id ‘submit’ is ‘hidden’ in HTML then an ElementNotVisibleException will be thrown.

}catch(ElementNotVisibleException e){

In this case, the exception occurs even if the page has not loaded completely.


A stale element means an old element or no longer available element. Suppose there is an element that is found on a webpage referenced as a WebElement in WebDriver. If the DOM changes then WebElement goes stale. If we try to interact with an element that is stale then the StaleElementReferenceException will occur.

For example:

WebElement firstName = driver.findElement(By.id(“first_name”));

In the code above, when we create the object first_name then the window switches. Then, WebDriver tries to type ‘finch’ in the form field. In this case, we see StaleElementReferenceException.


This exception occurs when there is not enough time for a command to be completed i.e. the element searched wasn’t found at the specific time.

For example:

driver.get(“https://www.example.com” );

In the above program, an implicit wait includes 10 seconds. If the page “www.example.com” doesn’t load in 10 seconds, then a TimeoutException will occur.


This exception occurs when a method is called after quitting the browser by WebDriver.quit(). This can also happen because of WebDriver issues like crashes.
For example:

Select dropdown = new Select(driver.findElement(By.id(“element”)));

WebDriver Exception:

This exception occurs when the WebDriver is acting right after you close the browser. Example: Suppose the path you have provided for the chrome driver doesn’t match with the correct path or the chrome version doesn’t match with the chrome version installed in Windows/Linux.


Exception handling is one of the important parts of selenium script. We can build robust and optimal code by handling exceptions in smart ways. Also, programmers can handle standard exceptions using various techniques i.e. try-catch, finally, throws, and others depending upon the requirements of scripts. For analyzing the exception in detail, one can also use methods like printStackTrace(), toString() and getMessage().





Written by 

Himanshu is an intern at Knoldus Inc. practicing in Test Automation Studio. He is familiar with the concepts of manual testing and automated testing using Cypress and Postman. He is always eager to learn new and advanced concepts in order to upskill himself. He likes to watch marvel movies and loves to play Badminton.