"No Such Element Exception" Error in Selenium Automation

Modified on Tue, 28 Mar, 2023 at 4:16 PM

Error Manifestation: "No Such Element Exception" Error is seen while executing Selenium Automation code.

Understand the error: "No Such Element Exception" is a common error that occurs when Selenium cannot locate an element on the web page. This error occurs when the script tries to interact with an element on the page, but the element is not present or cannot be found by the web driver.

Causes:

  • The element may not be present on the page.

  • The element may not have loaded on the page before the script executed.

  • The element may have been located using an incorrect locator strategy.

  • The page may have changed since the script was last executed.

Check: Can you check where this error is occurring in your code?(Exact file name and line no.)

Check: Check if any of the above causes is applicable for your code? Why do you think the error is being thrown on this line?

Methods for Debugging the Error:

Debug 1: Check for presence of element

  1. Check if the element is present on the page manually.

  2. If the element is present, check if it has loaded properly or not.

  3. If the element is not present, wait for it to load and try again.

Debug 2: Check for incorrect locator usage

  1. For the locator type you have chosen, ensure that the value is correct.

  2. If the element's ID, name, class, or CSS selector has changed, update it in the script.

Debug 3: Check for correct wait usage.

  1. If the element is taking time to load, use explicit waits to wait for the element to load.

  2. Use WebDriverWait and ExpectedConditions to wait for the element to be present on the page before interacting with it.

Debug 4: Handle error gracefully(Note : This might not solve your problem. But it will help you understand it more clearly.)

  1. Use try-catch blocks to handle the "No Such Element Exception."

  2. When the exception occurs, the script should handle it gracefully and retry again.

Code Snippets for reference:

Snippet 1:

//Verify the Element is Present on the Page if(driver.findElements(By.id("elementId")).size() != 0) { WebElement element = driver.findElement(By.id("elementId")); //Perform operation on element } else { //Wait for the element to load WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("elementId"))); //Perform operation on element }

Snippet 2:

//Use Appropriate Locator Strategy WebElement element = driver.findElement(By.cssSelector("cssSelector"));

Snippet 3:

//Use Explicit Waits WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("elementId")));

Snippet 4:

//Use Try-Catch Blocks try { WebElement element = driver.findElement(By.id("elementId")); //Perform operation on element } catch (NoSuchElementException e) { //Wait for the element to load WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("elementId"))); //Perform operation on element }



Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article