Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Selenium Webdriver Exceptions

DZone 's Guide to

Selenium Webdriver Exceptions

Learn about the different exceptions in Selenium Webdriver and how to handle them.

· Performance Zone ·
Free Resource

What Is an Exception?

An exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program’s instructions or in simple words, any issue which makes your test case stop in the course of the execution.

When an exception occurs, the normal flow of program halts and an exception object is created. The program then tries to find someone that can handle the raised exception. The exception object contains a lot of debugging information, such as method hierarchy, the line number where the exception occurred, the type of exception, etc. The process of creating the exception object and handing it over to the run-time environment is called “throwing the exception.”

Common Exceptions in Selenium Webdriver

There is a complete list of exceptions in Selenium Webdriver mentioned in the Selenium docs, which you may or may not encounter in the course of your testing. Hence, in this article, we will focus on some of the most common exceptions in Selenium Webdriver.

  • ElementNotVisibleException: Although an element is present in the DOM, it is not visible (cannot be interacted with). For example: Hidden Elements – defined in HTML using type=”hidden”.
  • ElementNotSelectableException: Although an element is present in the DOM, it may be disabled (cannot be clicked/selected).
  • InvalidSelectorException: Selector used to find an element does not return a WebElement. Say XPath expression is used which is either syntactically invalid or does not select WebElement.
  • NoSuchElementException: Webdriver is unable to identify the elements during runtime, i.e. FindBy method can’t find the element.
  • NoSuchFrameException: Webdriver is switching to an invalid frame, which is not available.
  • NoAlertPresentException: Webdriver is switching to an invalid alert, which is not available.
  • NoSuchWindowException: Webdriver is switching to an invalid window, which is not available.
  • StaleElementReferenceException: The referenced element is no longer present on the DOM page (reference to an element is now Stale). For example: the element belongs to a different frame than the current one OR the user has navigated away to another page.
  • SessionNotFoundException: The Webdriver is performing the action immediately after ‘quitting’ the browser.
  • TimeoutException: The command did not complete in enough time. E.g. the element didn’t display in the specified time. Encountered when working with waits.
  • WebDriverException: Webdriver is performing the action immediately after ‘closing’ the browser.

What Is Exception Handling?

Exception handling refers to the anticipation, detection, and resolution of exception, i.e. the block of code that processes the exception object.

How to Handle Exceptions in Selenium Webdriver

Try/Catch: A method catches an exception using a combination of the try and catch keywords. Try is the start of the block and Catch is at the end of try block to handle the exceptions. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following:

try {
 // Some code
} catch (Exception e) {
 // Code for Handling the exception
}

Below methods can be used to display Exception information:

  • printStackTrace(): prints the stack trace , exception name and description.
  • toString(): returns a text message describing the exception name and description.
  • getMessage(): displays the description of exception

Hope this article can be a quick reference when you need to look at some of the common exceptions in Selenium Webdriver.

Topics:
exceptions ,performance ,tutorial ,selenium webdriver ,selenium ,testing

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}