NoSuchElementException in Webdriver

Selenium NoSuchElementException is the most and frequent facing exception. You will definitely find this kind of exception while automating the application. In this blog we will see what are the reasons to face this exception and how we can overcome this exception in detail.

 

Below are the reasons to get this exception:

  • The locator which you are using to locate the element is incorrect/changed.
  • The element which you are looking is not rendered.
  • Trying to find the element before it was rendered.

 

Locator is Incorrect/changed:

Here we have two possibilities that the locator which you have written to identify an element may be incorrect or sometimes after writing/capturing the code, if there are any changes in the application then the locator may change. In both scenarios tool will not able to identify the element and it will throw NoSuchElementException.

 

To overcome this, we need to take the element locator which is causing this issue and validate against the application that the locator is identifying/highlighting any element on the application or not?

For this we can use Firefox add-ons i.e. firebug and firepath then you place the locator in the firepath and validate the same against application that it is highlighting the element or not?

 

And sometimes if you are not navigated to a particular page after clicking on a link or button then it will cause this issue as it will look for the element to interact in the new page but actually it is not navigated to that page.

 

But here the actual error show that it is not able to find particular element but the issue is not with that element as the page not yet navigated to that page.

 

This kind of situation is bit tricky, but we need to find these type of issues also. Best example for this is navigating to application using login page but if the credentials are wrong then you might get this issue.

 

To overcome this issue, we can capture the screenshots for these type of scenarios and can find the issues by looking into these screenshots.

 

Element Not Rendered:

It is bit difficult to find this as something is not rendering on the application then you need to debug the code and see what could be the real issue. If something is not rendering then that is a bug in the application.

 

Here if you use the first issue resolution then it will not work as if you try to highlight the element in the application using firebug and firepath then it will not show anything in the console. But this is not an issue with the locator instead the element is not rendering on the application.

To overcome this issue, we can capture the screenshots for these type of scenarios and can find the issues by looking into these screenshots.

Trying to find the element before it was rendered:

Sometimes we are trying to interact with an element which is not yet rendered on the application. Here also we will get the same kind of issue which we saw in the above. But in the above case the element is not completely rendering, here it is rendering but taking bit more time to load.

 

Now a days lots of modern websites use technologies such as jQuery or AngularJS, which use the JavaScript to manipulate the DOM. And Our Selenium is very fast. In many cases, it’s ready to start interacting with your website before the JavaScript has finished doing its job.

To overcome this, we need to think that how a manual tester will test these kind of scenarios. They will wait until the element renders on the application. So, same way we need to wait to load that particular element.

 

For this we will use Synchronization concept i.e. we need to stop the code execution until the element renders. For this we can use ExpectedConditions class and its methods to achieve this functionality.

Leave a Reply

Your email address will not be published. Required fields are marked *