2

I'm getting this error:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: 
Unable to locate element

Why am I getting this WebDriver exception and how can I fix it?

  • 1
    You're trying to locate an element that does not exist on the page. – FDM May 14 '16 at 9:25
  • absolutely right @FDM – Bharat Mane May 14 '16 at 9:51
  • 1
    Is it really easier to post a question on Stack Exchange than to read the documentation? – Aaron Miller May 14 '16 at 12:33
  • Reading is less fun. Instead, get a few sock puppets, ask question, upvote it, get few answers, upvote those too. All the while creating worthless questions/answers... – Peter M. May 16 '16 at 14:36
4

You will get this exception when WebDriver Is not able to locate element on the page of software web application using whatever locator you have used In your test. To resolved this Issue, I will check bellow given things.

First of all I will check that I have placed Implicit wait code In my test or not. If you have not placed Implicit timeout In your test and any element Is taking some time to appear on page then you can get this exception. So I will add bellow given line at beginning of my test case code to wait for 15 seconds for element to be present on page. In 70% cases, this step will resolved Issue.

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

If Implicit wait Is already added and element locator Is fine then you need to verify that how much time It(element) Is taking to appear on page. If It Is taking more than 15 seconds then you have to put explicit wait condition with 20 or more seconds wait period as bellow. In 5 to 10% cases, This step will resolve your Issue.

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#submitButton")));
3

The reason behind the exception you have mentioned is because with your test script you are trying to locate an element that doesn't exist in the webpage you are trying to test.

how will you resolve It?

Well, along with the text you have provided, the console would also have given the line number in the test script where this exception occurred. I would go to that line in the test script and check what I've done wrong.

  • Have I provided a wrong value for searching the element?
  • Is the method wrong?
  • Am I missing something?

If answer to all these question is No and the element is indeed available in the webpage, I would apply implicit or explicit wait in the script to allow Selenium to wait for the element to load and then read it.

You can find the tutorials for implicit and explicit waits on the official Selenium website.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.