Google+

Different types of Selenium WebDriver Exceptions


The below digram depicts the different types of Exceptions that we commonly face while working with Selenium WebDriver:



Before understanding the different types of  Exceptions which we may face while working with Selenium WebDriver, first I will explain the term Exception.

What is an Exception ?

Exception is a problem which occurs during the execution of the program and when an Exception is occur the program execution will stop and the rest of the code in the program wont be executed.

Example for Exception:

Lets say, we have 5 lines in our Program as shown below and if an exception occurs at 3rd line, the program execution will stop and the remaining lines i.e. 4th and 5th in our program wont be executed.

Statement1;
Statement2;
Statement3;  //Exception occurred at this 3rd line
Statement4;  //This line wont be executed
Statement5;  //This line wont be executed

Example for an Exception in Java:

Java Program Code:

Read the comments in the below Java program code:


Output:

The below output will be displayed in the Eclipse IDE console. From the output, its very clear that the Program execution stopped due to ArithmeticException and the last statement in the above program didn't get executed:



Example for Handling an Exception in Java:

In the above program, if we want to continue the execution of the Java program, even after the exception occurs, we have to handle it.  The method of handling the exception is known as Exceptional Handling.

Surround the Java Statement which is throwing the Exception using 'try ... catch' block as shown in the below screen-shot:

Note: catch block in 'try .. catch' will use the respective Class which is required to handle the Exception. Since the exception thrown in the above program is ArtihmeticException, we have a predefined class in Java known as ArithmeticException which can handle this exception.



Now, execute the Exception handled code and observe that Exception will be handled and program will continue to execute without stopping when the exception occurs. The below output displayed in the Eclipse IDE console is the proof that the exception got handled and the program continued to execute:


If you still want to understand Exceptions and their handling mechanism in a detailed manner, you can go through the following posts:



Different types of Exceptions in Selenium WebDriver 

From the above examples, you understand what is an Exception and what happens when it occurs during program execution. Now, I will explain all the different types of Exceptions, we may face while working with Selenium WebDriver.

Though there are many WebDriver Exceptions in Selenium, the below are the different WebDriver Exceptions that we commonly face while working with Selenium:
  1. NoSuchElementException
  2. ElementNotVisibleException
  3. NoSuchFrameException
  4. NoAlertPresentException
  5. NoSuchWindowException
  6. SessionNotFoundException
  7. StaleElementReferenceException
Now, I will explain all these Selenium WebDriver Exception in a detailed and practical way:



NoSuchElementException

This Exception occurs, when the locators (i.e. id / xpath / css selectors etc) we mentioned in the Selenium Program code is unable to find the web element on the web page. There are two possibilities for getting this Exception, i.e. either we have provided a incorrect locator and trying to find the web element or we have provided correct locator, but the web element related to the locator is not available on the web page.

Click here to understand NoSuchElementException in a detailed and practical manner.



ElementNotVisibleException

This Exception occurs, when the locators (i.e. id / xpath / css selectors etc) we have provided in the Selenium Program code is trying to find the web element which is in hidden from displaying on the page. For example, there is no button displayed on the web page and if there is HTML code related to the button, then on trying to find the button using locators of the button by executing the Selenium Code, we get ElementNotVisibleException .

Click here to understand ElementNotVisibleException in a detailed and practical manner.



NoSuchFrameException

iframe is a HTML web page inside another HTML web page. In order to work with the Web Elements on any iframe, we have to first switch to the iframe in Selenium and then locate the respective web elements inside the iframe. NoSuchFrameException WebDriver Exception occurs, when the driver in the Selenium Program code is unable to find the frame on the web page to switch. i.e. when the driver is switching to an invalid or non-existing iframe.

Click here to understand NoSuchFrameException in a detailed and practical manner.



NoAlertPresentException

Alert is a type of pop-up, which pops up to provide important information to users. In order to work with Alert pop-ups, we have to first switch to Alert and then perform operations on Alert like reading the messages on the Alerts or Accepting the alert by pressing 'OK' button on the alert etc. NoAlertPresentException WebDriver Exception occurs, when the driver in the Selenium Program code is unable to find the Alert on the web page to switch. i.e. when the driver is switching to an invalid or non-existing Alert pop-up.

Click here to understand NoAlertPresentException in a detailed and practical manner.



NoSuchWindowException

As Selenium only automates Web Applications, we will be mostly dealing with Browser Windows. Browser Window is a square box in which browser generally display the web pages in it. In order to work with the Web Elements on any Browser Pop-up Window, we have to first switch to the pop-up  in Selenium and then locate the respective web elements inside the pop-up window. NoSuchWindowException WebDriver Exception occurs, when the driver in the Selenium Program code is unable to find the pop-up window on the web page to switch. i.e. when the driver is switching to an invalid or non-existing pop-up window.

Click here to understand NoSuchWindowException in a detailed and practical manner.



SessionNotFoundException

This Exception will occur when driver is trying to perform operations on the Web Application after the Browser is closed.

Click here to understand SessoinNotFoundException in a detailed and practical manner.



StaleElementReferenceException WebDriver Exception

StaleElementRefereneException WebDriver Exception occurs mainly because of page navigations in between the execution of Selenium code. i.e. This exception occurs, when Selenium navigates to a different page, come backs to the same old page and performs operations on the old page. Technically, it occurs when the element defined in the Selenium code is not found in the cache memory and the Selenium code is trying to locate it. When we define an element on a page using Selenium, Selenium stores it in a cache memory. The element stored in cache memory generally gets deleted when the driver navigates away to another page during Selenium code execution. On coming back to the same old page and then trying to identify the cache removed element on the old page, we get StaleElementReferenceException as a result.

Click here to understand StaleElementReferenceException in a detailed and practical manner.



Hence these are the most common seen Selenium WebDriver exceptions.

4 comments:

Unknown said...

Hi Arun, nice work..thanks!

Regards,
Avanthi Ravula

Arun Motoori said...

Thanks for your feedback Avanthi Ravula :)

Unknown said...

Hi arun nice explanation keep going on for java concepts also very clear and understandable thanks for preparing like this for us

Arun Motoori said...

Thank you Bhargav Chandra :)