Updated April 3, 2023
Introduction to Selenium Exceptions
The following article provides an outline for Selenium Exceptions. The selenium exception is one of the features. It is a regular occurrence for Selenium WebDriver during the execution of automation code; also, the exception name itself it implies or unusual occurrences for numerous reasons. The most important point is to catch the issues that will handle the result of the exception handling for anticipating process and intelligently handle the mechanism for unusual problems before they occur and these errors have occurred at a variety of reasons which includes the syntax faulty, erroneous parameters passing the values or lack of specific features which needed on the application.
What are Selenium Exceptions?
- When an element is provided in the DOM, but we are unable to select it for selenium exception happens, and therefore it is not feasible for to interact with web elements that can be separated from the current DOM a selenium exception occurs during this session it also throws SessionNotFoundException once the browser is closed the webdriver starts its own activity.
- In general, the exception occurred at the time of program execution; while we run the program, it generates the exception that should be handled to avoid the program crash on the application.
How to Use Selenium Exceptions?
- In general, selenium webdriver follows some standard ways for to handle the exceptions in Selenium WebDriver. With the help of try-catch, multiple catch blocks, Throw/Throws, these are some ways to handle the selenium exceptions during compile and runtime mechanism. If suppose we have used, try-catch it’s one of the methods, and it can catch the exceptions by using a combination of the try and catch keywords. The try block is the initial or start of the block, and the catch method is located at the end of the try block for handling the resolved exception. Basically, the try-catch blocks are referred to as the protected code.
- So that if we use Multiple catch blocks, the exception will occur more than once from a single block of code, but multiple catch blocks are used for to handle every kind of exception separately with some block of code. The two catch blocks have no limitation for calculating the number of catch blocks. The Throw and Throws keywords are the most important for programmers to generate the exception both implicitly and explicitly; the throw keyword is used to throw the exception for runtime exception to handle it. If suppose the coder cannot handle the exception; then it will be used as the Throws keyword in any method signatures. The finally block should be executed with or without a catch block in the code.
Types of Selenium Exceptions
It has n number of types some of them are as follows:
- WebDriver Exception: This exception is acting more important, and it will follow immediately after open and close the browser during program execution.
- Timeout Exception: We know about the exception; it will perform only the time duration, so if the user operation is unable to reach the time, it shows the timeout exception. It is also called during the application and program execution; the thread automatically created, activated, and destroyed the thread life cycle will be executed and rotated continuously while the program ends.
- Session Not Found Exception: This exception is more important for the web drivers that will be acted immediately after existing the browsers.
- StaleElementReferenceException: It is mostly used in rare scenarios, so the DOM elements are not present in the stable state.
- NoSuchWindowException: The webdriver exists only; it will switch the invalid windows that are presented during the program execution.
- NoAlertPresentException: It also validated using the webdriver for switching the invalid window, which is not present in the application.
- NoSuchFrameException: The webdriver will validate and attempt to switch over the frames which are not presented on the application window.
- NoSuchElementException: It is mainly covered under the finding elements, which are not presented on the webdriver.
- ElementNotSelectableException: Only this exception will occur if the web element of click, select, choose UI elements is to be disabled or un-selected that is available on the DOM.
- ElementNotVisibleException: This exception will occur only the element type or attribute is a hidden mode, then only this exception throws else, it does not happen.
Selenium Exception Methods
We know the types of exceptions that occurred at each stage of the program execution. But in normal time, it has been checked, and un-checked exceptions are the two main types of exceptions that occur at compile and runtime.
- Checked Exception: It occurs only at compile time; it has some child types.
- Un-Checked Exception: It only happens during runtime.
Example of Selenium Exception
Different example is mentioned below:
Example #1
Code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class New {
public static void main(String[] args) throws InterruptedException{
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Kripya-PC\\Downloads\\chromedriver_win32 (1)\\chromedriver.exe");
WebDriver dr = new ChromeDriver();
dr.manage().window().maximize();
String strs="http://www.testyou.in/Login.aspx";
dr.get(strs);
dr.findElement(By.id("abc")).sendKeys("sdvg");
dr.quit();
}
}
Output:
In the above example, we reproduced no such element found exception in the web application. Each web page will calculate the ids and names for accessing the web elements in the selenium drivers interfaces. If the element is not matched or wrongly mentioned in the Xpath, it will throw the no such element found exception.
Example #2
Code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class New {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Kripya-PC\\Downloads\\chromedriver_win32 (1)\\chromedriver.exe");
WebDriver dr = new ChromeDriver();
dr.get("https://in.search.yahoo.com/?fr2=inr");
dr.manage().window().maximize();
Thread.sleep(2000);
WebElement wb=dr.findElement(By.xpath("//a[@id='ysignin']"));
wb.click();
dr.navigate().back();
Thread.sleep(2000);
wb.click();
}
}
Output:
In the above example, we reproduced the exception called StaleElementReferenceException for performing the sign-in operations on the yahoo web application. We used thread count to perform each operation and count the time frame for spending the stable position on each page. We used default methods for navigating and clicking the page in the application.
Conclusion
In the selenium framework, the automation scripts are used to save time in the application productivity and the client satisfaction for their business needs. Therefore, we used different features classes, methods, variables, and keywords to perform the operations and make the application productivity as user satisfaction.
Recommended Articles
We hope that this EDUCBA information on “Selenium Exceptions” was beneficial to you. You can view EDUCBA’s recommended articles for more information.