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.
Example: NoSuchElementException
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.
1. ElementNotVisibleException
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.
Root Cause:
- HTML attribute set to ‘hidden’
2. NoSuchElementException
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.
Root Cause:
- Locators of the web element may have changed
- Elements are removed from the DOM
- Their coordinates have changed
3. ElementNotSelectableException
As its name says, WebDriver is not able to select or click the web element on Application under test (AUT).
Root Cause:
- The attribute of the web element is set to disable
4. NoSuchWindowException
Selenium WebDriver is unable to switch to the window.
Root Cause:
- The possible root cause could be the invalid address of the window
- Switchable window does not exist
5. NoSuchFrameException
WebDriver is unable to switch to the given frame.
Root Cause:
- Invalid address of the frame
- Switchable frame does not exist
6. NoAlertPresentException
Selenium WebDriver is unable to switch to the alert.
Root Cause:
- Alert does not exist at particular session
7. InvalidSelectorException
When you write wrong XPath syntax then this exception may occur.
Root Cause:
- Syntax is not correct
8. TimeoutException
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.
Root Cause:
- 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
10. StaleElementReferenceException
This exception occurs when the web element gets detached from the current DOM.
Root Cause:
- Sometimes it occurs due to slow server response
11. SessionNotFoundException
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.
12. InvalidCoordinatesException
This exception occurs when you write command to perform the action by specifying coordinates and WebDriver unable to act as per given coordinates.
13. UnableToSetCookieException
This exception occurs when WebDriver fails to set the cookie in the browser.
14. UnhandledAlertException
When an alert is present, but WebDriver unable to perform the Alert operation then UnhandledAlertException occurs.
15. InvalidCookieDomainException
When the user gives commands to insert cookie which is having the different domain name then InvalidCookieDomainException occurs.
16. UnreachableBrowserException
This exception occurs when the browser does not open or due to some reason browser may have crashed.
17. UnsupportedCommandException
When remote WebDriver send invalid command then UnsupportedCommandException occurs.
18. ConnectionClosedException
This exception occurs when there is a connection loss in the driver.
19. MoveTargetOutOfBoundsException
This exception occurs when the defined target does not exist in the given size.
20. NoSuchContextException
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.