An exception is a much-known word in the programming world, it doesn’t matter you are writing codes in which language. Exceptions are like the villain for programmers, but it is far better than any bug or error because it throws logical termination. We will discuss the various exceptions in Selenium WebDriver, but before that let me explain you the real technical knowledge about Exceptions.
What is an Exception?
It is a logical event which is mainly thrown at runtime that disrupts the normal flow of a program. In general, we say this is an abnormal condition of the code or logic written in the programming language.
Recommended Readings: How to handle multiple windows in Selenium?
Classification of Exception
Exceptions are classified in mainly two types, Checked Exception and Unchecked Exception.
Let’s discuss each of the classifications in details:
1. Checked Exception
Checked Exceptions are the exceptions which are handled while writing codes. These exceptions are handled before compiling the code; hence, such exceptions are checked at the compile time.
Example: WebDriver Exception
2. Unchecked Exception
These are the exceptions which are thrown at runtime. Runtime exceptions are worse than compile time exception. It creates trouble while running Automation pack in headless.
List of Exceptions in Selenium WebDriver
Below are the lists of major exceptions in Selenium WebDriver which occur at compile time as well as run time. Today we will only discuss the types of exceptions which are coming in Selenium WebDriver and in next tutorial we will learn to handle these exceptions in Selenium WebDriver with Java programming.
This type of Exception occurs when existing element in DOM has an attribute set as hidden. In this case, elements are there, but they are not visible or available to interact with the WebDriver.
- HTML attribute set to ‘hidden’
WebDriver is not able to find the existence of web element on the web page. Here, the findBy method is unable to find the web element.
- Locators of the web element may have changed
- Elements are removed from the DOM
- Their coordinates have changed
As its name says, WebDriver is not able to select or click the web element on Application under test (AUT).
- The attribute of the web element is set to disable
Selenium WebDriver is unable to switch to the window.
- The possible root cause could be the invalid address of the window
- Switchable window does not exist
WebDriver is unable to switch to the given frame.
- Invalid address of the frame
- Switchable frame does not exist
Selenium WebDriver is unable to switch to the alert.
- Alert does not exist at particular session
When you write wrong XPath syntax then this exception may occur.
- Syntax is not correct
This exception mainly occurs when you use to wait commands like Implicit Wait, Explicit Wait or Fluent Wait. Once any of the waits fails after exceeding defined time then TimeoutException occurs.
- WebDriver fails to perform activity in defined time
9. WebDriver Exception
This exception occurs when WebDriver performs its action after immediately closing the browser. Example: Try to use an older version of ChromeDriver.Exe with latest Google Chrome and latest Selenium JAR file then WebDriver will give WebDriver Exception.
Don’t Miss: Top 10 Browser for Web Surfing
This exception occurs when the web element gets detached from the current DOM.
- Sometimes it occurs due to slow server response
This exception occurs when webdriver starts new action without giving delay after finishing the previous action. In simple terms, WebDriver performs the new action after immediately quitting the browser which was running in the previous session.
This exception occurs when you write command to perform the action by specifying coordinates and WebDriver unable to act as per given coordinates.
This exception occurs when WebDriver fails to set the cookie in the browser.
When an alert is present, but WebDriver unable to perform the Alert operation then UnhandledAlertException occurs.
When the user gives commands to insert cookie which is having the different domain name then InvalidCookieDomainException occurs.
This exception occurs when the browser does not open or due to some reason browser may have crashed.
When remote WebDriver send invalid command then UnsupportedCommandException occurs.
This exception occurs when there is a connection loss in the driver.
This exception occurs when the defined target does not exist in the given size.
This type of exception mainly occurs in mobile device testing. It is thrown by ContextAware.
These are the list of 20 major exceptions in Selenium, if you have any queries then feel free to write in comment below.