11 Most common Exceptions in Selenium WebDriver

Everything is not easy at the beginning. Once you start your Selenium automation journey, you will frequently encounter certain exceptions in Selenium WebDriver. But don’t worry, we got you covered. It’s good to have a basic knowledge about 11 of the most common Exceptions in Selenium WebDriver which you may / may not encounter during the course of your test automation.

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 between the execution.”

When exception occurs, the normal flow of program halts & 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, line number where the exception occurred, type of exception etc. The process of creating the exception object and handing it over to 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 Doc which you may or may not encounter in course of your testing. Hence in this article we will focus on some most common exceptions in Selenium WebDriver,

ElementNotVisibleException: Although an element is present in the DOM, it is not visible (cannot be interacted with). E.g. 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 run time, 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). E.g. 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: The 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. Don’t forget to share it with your colleagues 🙂

Related Articles

Tables are one of the primary design tools for HTML documents. Tables allow for greater control over page layout, allowing creation of more visually interesting pages. Table has rows and columns to store the data. I guess 50% IT industry will come to a halt if Microsoft Excel stops working 😉 that’s the importance of tables in organizing data. Search results on many websites are often displayed in the form of table. E.g. try searching for a flight between X and Y on a travel website, what you get is a table of results. There are times when we need to access elements (usually texts) that are within HTML tables. In this article let’s see how to read data from a Web Table using Selenium.

Occasionally you will encounter the below error when trying to locate an element. One of the reason might be that the element is present within a frame. And when writing a test against them, you can easily get tripped if you’re not paying attention. You cannot directly identify an element present in an iFrame. First we have to switch to the frame and then we can identify the elements present using normal Selenium locators. In this article let’s see how to handle frames in Selenium Webdriver.

Error:

Exception in thread “main” org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {“method”:”id”,”selector”:”tinymce”}

After successfully running several automation scripts now we are stepping towards grasping the essential ‘Get’ commands in Selenium. We will precisely and briefly discuss each of these Selenium commands so as to make you capable of using them effectively whenever required. Thus we are going to discuss about various driver.get in Selenium WebDriver that we would be using in our day to day automation testing. driver.get in Selenium WebDriver are used to collect various information about the page that we deal with. Here are some important “get” commands you must be familiar with,

About STS

Software Testing Studio is an attempt to share some incredible knowledge from industry leaders & experts, which should be helpful for anybody to start his/her career in ‘Software Testing’ or to progress it further. Apart from the technical nitty-gritties, one can also find some intellectual posts by industry experts sharing their Wisdom.