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.
try{
//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{
try{
//code
} catch(Exception e){
// Do whatever you wish to do here
// Now throw the exception back to the system
throws(e);
}}
- Multiple Exceptions: A programmer can declare various exceptions in the throw clause.
public void main function_name() throws ExceptionType1, ExceptionType2{
try{
//code
} 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.
try{
//code
}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:
NoSuchElementException:
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.
try{
driver.findElement(By.id(“submit”));
}catch(NoSuchElementException e){
System.out.println(e.getMessage());
}
In this case, the exception occurs even if the element is not present.
NoSuchWindowException:
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.
driver.switchTo().window(handle_1);
ElementNotVisibleException:
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.
driver.findElement(By.id(“submit”));
Handling:
try{
driver.findElement(By.id(“submit”));
}catch(ElementNotVisibleException e){
System.out.println(e.getMessage());
}
In this case, the exception occurs even if the page has not loaded completely.
StaleElementReferenceException:
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”));
driver.switchTo().window(Child_Window);
element.sendKeys(“finch”);
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.
TimeoutException:
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.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
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.
NoSuchSessionException:
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:
WebDriver.quit();
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.
CONCLUSION
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().
References:
https://www.guru99.com/exception-handling-selenium.html
