Home Guide Exception Handling in Selenium WebDriver

Exception Handling in Selenium WebDriver

By Jash Unadkat, Technical Content Writer at BrowserStack -

What is an Exception?

“Exception” is a standard term used by software programmers regardless of any programming language that is used to write the code. “Exception” as the name suggests is an unusual event or an uncommon case that disrupts the normal flow of program execution.

There may be several reasons behind the occurrence of exceptions which indicate the halt in the program flow. An exception object is created when an exception is encountered, which contains debugging information such as the line number, the type of Exception, the method hierarchy. Once the exception object is created and handed over to the runtime environment, this process is called “Throwing the Exception.”

The Exceptions are classified mainly into two types: Checked Exception and Unchecked Exception

  • Checked Exceptions: These exceptions are handled while writing the code itself before the compilation of the code, and hence they are examined at the compile time.
  • Unchecked Exceptions: These exceptions get thrown at run time and are more catastrophic as compared to Checked Exception.

What is Exception Handling in Selenium?

Exception handling is a process or a mechanism that detects and resolves runtime exceptions and communication errors in Selenium.

10 Common Exceptions in Selenium WebDriver

One can find a complete list of Selenium WebDriver Exceptions in the documentation given by Selenium, but below are a few standard Selenium exceptions faced while running a test :

  1. ElementNotVisibleException: In spite of the element being present in the DOM, it is not visible (can not be interactive). For example, elements defined in HTML with type =”hidden”
  2. ElementNotSelectableException: An element is disabled (can not be clicked/selected) in spite of being present in the DOM
  3. NoSuchElementException: Webdriver is not able to determine the elements during runtime, i.e., the FindBy method cannot find a particular component
  4. NoSuchFrameException: Webdriver attempts to switch to an invalid frame, which is unavailable
  5. NoAlertPresentException: Webdriver is trying to switch to an invalid alert, which is unavailable
  6. NoSuchWindowException: Webdriver is trying to switch to an invalid window, which is unavailable
  7. StaleElementReferenceException: The referenced element is no longer present on the DOM page (a reference to a component is now Stale). For example, the item belongs to a different frame than the current one or the user has navigated away to another page
  8. SessionNotFoundException: Webdriver is acting immediately after ‘quitting’ the browser
  9. TimeoutException: The command did not complete in the specified time. For example, the element didn’t display at the specified time. This is especially encountered when working with waits
  10. WebDriverException: Webdriver is acting immediately after ‘closing’ the browser

Now, as we are aware of the common Exceptions that one can face in Selenium WebDriver, the next step is to understand “How to handle those Exceptions?

Handling Exceptions In Selenium WebDriver

Following are a few standard ways using which one can handle Exceptions in Selenium WebDriver:

Try-catch: This method can catch Exceptions by using a combination of the try and catch keywords. Try indicates the start of the block, and Catch is placed at the end of the try block to handle or resolve the Exception. The code that is written within the Try/Catch block is referred to as “protected code.” The following code represents the syntax of Try/Catch block –

try

{

// Some code

}catch(Exception e){

// Code for Handling the exception

}

Multiple catch blocks: There are various types of Exceptions, and one can expect more than one exception from a single block of code. Multiple catch blocks are used to handle every kind of Exception separately with a separate block of code. One can use more than two catch blocks, and there is no limitation on the number of catch blocks. The code below represents the syntax of multiple catch blocks –

try

{

//Some code

}catch(ExceptionType1 e1){

//Code for Handling the Exception 1

}catch(ExceptionType2 e2){

//Code for Handling the Exception 2

}

Throw/Throws: When a programmer wants to generate an Exception explicitly, the Throw keyword is used to throw Exception to runtime to handle it. When a programmer is throwing an Exception without handling it, then he/she needs to use Throws keyword in method signature to enable the caller program to understand the exceptions that might be thrown by the method. The syntax for Throws is as follows:

public static void anyFunction() throws Exception{

try{

// write your code here

}catch (Exception e){

// Do whatever you wish to do here

// Now throw the exception back to the system

throw(e);

}

}

Multiple Exceptions: One can mention various Exceptions in the throws clause. Refer to the example below:

public static void anyFunction() throws ExceptionType1, ExceptionType2{

try{

// write your code here

}catch (ExceptionType1 e1){

// Code to handle exception 1

}catch (ExceptionType1 e2){

// Code to handle exception 2

}

Finally: The Final keyword is used to create a block of code under the try block. This final code block always executes irrespective of the occurrence of an exception

try{

//Protected code

}catch(ExceptionType1 e1){

//Catch block

}catch(ExceptionType2 e2){

//Catch block

}catch(ExceptionType3 e3){

//Catch block

}finally{

//The finally block always executes.

}

One can also use the following methods to display Exception Information:

  • printStackTrace(): It prints the stack trace, name of the exception, and other useful description
  • toString(): It returns a text message describing the exception name and description
  • getMessage(): It displays the description of the exception

Try Exception Handling in Selenium Webdriver

Conclusion:

  • Exception handling is a very crucial part of every Selenium Script
  • Exceptions can not be ignored as they disrupt the normal execution of programs
  • One can write optimal and robust scripts by handling the Exceptions in unique ways
  • Programmers can handle standard exceptions using various techniques like Try/catch, Multiple catch blocks, Finally, and others depending upon the requirement of scripts
  • For analyzing the exceptions in detail, one can also use methods like printStackTrace(), toString(), and getMessage()

BrowserStack Logo Run Selenium Tests on 2000+ Browsers & Devices Get Started Free