Getting 'org.openqa.selenium.NoSuchElementException' - how to fix?



  • 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?



  • 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")));
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2