NoSuchElement exception, selenium webdriver is showing an exception as element not found in the web page.

When a web driver is not able to find the required web element in the web page, then the script will through an exception called no such element exception.

In order, to run the script and catch the exception, we need to use try and catch blocks for the code.

Example:

Webelement submitbutton = diver.findelement(By.xpath(“xpathbuttonsubmit”));

The above line in the selenium automation is throwing an exception No such element exception.

To handle it we need will use try and catch blocks, we will also use the implicit waits to for webpage to load completely and to identity the web element. If the web element is not found, it will throw an exception which is technically a bug in the code or application.

Example:

try{

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

Webelement submitbutton = diver.findelement(By.xpath(“xpathbuttonsubmit”));

}catch(NoSuchElementexception a)

{

System.out.println(“The exception: “ +a)

}

From the above modified code, we can see that

  1. Implicit wait
  2. Try and catch block with no such element found exception.
  3. Handling the exception and printing in the console to run the code completely
  4. If the exception is found, then it is an error in the code or application.
  5. Check the web element details once and run again to confirm the error is in the application.

Leave a Comment

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