What is 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 that 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, and the method hierarchy. Once the exception object is created and handed over to the runtime environment, this process is called “Throwing the Exception.”
Table of ContentsTypes of Exceptions in Selenium
The Exceptions are classified mainly into two types:
- 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.
11 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:

- Try for other interfaces to select the element (selectByIndex, selectByVisibleText).
- A wait command can also be added to wait until the element becomes clickable.
- Wait until the element is visible or clickable using Explicit wait.
- Scroll until the element gets in display using the Actions class.
- Use JS Executor to interact directly with the DOM.
- Try to write unique locators that match with a single element only.
- Wait until the element is visible or clickable using explicit wait.
- Reverify the locator by inspecting the browser and checking whether the element is present on DOM or not. Try switching to more reliable locators.
- Wait for the element to load using explicit wait.
- Use wait after the action which triggers to open the iframe to ensure the iframe is loaded properly.
- Ensure the iframe locator is correct. (Switch between iframe name, id or index and recheck)
- Use wait after the action which triggers to open the Alert to ensure it is loaded properly before interacting.
- Ensure alert locator is correct and visible on DOM by inspected browser. Try with different alert locators.
- Ensure that the window handle or name being used is accurate.
- Wait for the browser window or tab to completely load and then switch the WebDriver to the desired window instance.
- Refresh the page before accessing the element that causes StaleElementException.
- Use try-catch block to handle the exception and attempt to locate the element again in the catch block.
- Check the load time of the web element manually and add wait accordingly.
- Add explicit wait using JavaScript executor until the page is loaded.
- Try using some other property to locate the element such as CSS Selector or Xpath.
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 the 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 Finally keyword is used to create a block of code under the try block. This finally 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 in Selenium 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()
ncG1vNJzZmivp6x7o77OsKqeqqOprqS3jZympmeXqralsY6er5ydoKm2sLrSZqCnZaOauaa6yK6kZq%2BVl7GztdWeqQ%3D%3D