NoSuchElementException WebDriver Exception

Before understanding NoSuchElementException, you have to first understand the below:

So, after understanding 'What is an Exception and the different types of Exceptions in Selenium WebDriver', you are now ready to understand NoSuchElementException in a detailed and practical way.

NoSuchElementException is one of the different WebDriver Exceptions and 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.

Practical Example for NoSuchElementExcpetion:

1. Open Firefox Browser and open as shown below:

2. Using FireBug and FirePath, inspect the hyperlink shown on the page and retrieve the XPath locator for helping the Selenium Code find the highlighted hyperlink on the web page, as shown below:

3. Now write the Selenium Code for clicking the highlighted hyper-link in the above image by using the yellow highlighted XPath locator as shown below and Execute it:

Note: The below program will work fine, as the XPath locator given is a valid one and the Web Element related to the XPath locator is available on the Web Page. Hence no Exception will be arise during the execution of the below Selenium Code.

4. Now, provide invalid XPath locator in place of the above yellow highlighted valid XPath locator as shown below and execute the code:

5. On execute the Selenium code in the above image having invalid XPath locator,  NoSuchElementException WebDriver Exception will be displayed in the Eclipse IDE as shown below:

6. Also observe that the Selenium Code execution will stop at the line where the NoSuchElementException occurred and the remaining lines of code didn't get executed as shown  below:

Handling NoSuchElementException :

1. If you want those statements to be executed, even after the Exception has occurred in the before statement, we need to handle the Exception using Exceptional Handling mechanism:

i.e. We have to use try .. catch blocks to handle the exception and also 'NoSuchElementException' WebDriver Exception Class needs to be used in the catch block as shown in the below code:

2. Hover the mouse over the 'NoSuchElementException' error in the above image and select 'import NoSuchElementException org.openqa.selenium ' option from the suggested resolutions as shown below:

3. Observe that the error gets resolved as shown below and execute the code:

4. Observe that the Exception got handled successfully and statements after the Exception got executed as a result of handling the exception. The below screen-shot is the proof that the NoSuchElementException is handled using Exception handling mechanism:

Hence NoSuchElementException Exception will be occurred, 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 and in order to handle this, we have to use NoSuchElementException WebDriver Class in the catch block.


Ganesh Gupta said...

Very neatly explained..


Arun Motoori said...

@Ganesh - Thank you very much :)

Unknown said...

Thanks a lot Arun for sharing knowledge

Arun Motoori said...

Thanks for your feedback :)