Exception Handling in Selenium WebDriver

In this tutorial, we will learn Exception Handling in Selenium WebDriver. Before learning about Exceptions in Selenium, I will recommend you to go through Exception Handling in Java to understand what is Exception and How it is handled in Java.

Exception Handling in Selenium WebDriver

There is a class called org.openqa.selenium.WebDriverException provided by Selenium WebDriver which inherits RuntimeException, Exception, Throwable and Object.

Selenium_WebDriver_Exception

WebDriverException class is then inherited by many other classes. Those are:

ConnectionClosedException, ErrorHandler.UnknownServerException, ImeActivationFailedException, ImeNotAvailableException, InvalidArgumentException, InvalidCookieDomainException, InvalidCoordinatesException, InvalidElementStateException, JavascriptException, JsonException, MoveTargetOutOfBoundsException, NoSuchSessionException, NotFoundException, ScreenshotException, ScriptTimeoutException, SessionNotCreatedException, StaleElementReferenceException, TimeoutException, UnableToCreateProfileException, UnableToSetCookieException, UnexpectedTagNameException, UnhandledAlertException, UnreachableBrowserException, UnsupportedCommandException, WindowsRegistryException

Frequently used Exception classes are:

NotFoundException:

  • This exception is thrown when we are trying to interact with some WebElement, Frame, Window, Alert etc. and they are not found on the page.

InvalidElementStateException:

  • ElementNotInteractableException – This exception is thrown to indicate that although an element is present on the DOM, it is not in a state that can be interacted with.

  • ElementNotSelectableException – This exception is thrown to indicate that although an element is present on the DOM, it is not selectable, and so is not able to be interacted with.

  • ElementNotVisibleException – This exception is thrown to indicate that although an element is present on the DOM, it is not visible, and so is not able to be interacted with.

Exception Handling in Selenium WebDriver

Go through all the Exception classes which are inherited by WebDriverException from Java Docs of Selenium WebDriver and apply them accordingly.

It is important that you ask questions to clear your doubts. Comment below if there is anything that you would like to discuss in detail.

PS: For any questions, queries or comments feel free to write us at support@qatechub.com or saurabh@qatechhub.com

Saurabh Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra: