1

No matter what search type I use, I always get "no such element found" error. Why does this happen?

public void CorrectPIN() throws InterruptedException{
    driver.findElement(By.id("identifier")).sendKeys("abhisingh1313@mailinator.com");
    driver.findElement(By.id("button")).click();
    Thread.sleep(5000);
     do {
        Thread.sleep(100);
    } while (driver.findElement(By.id("pin")).isDisplayed());
}

.................. I am getting unable to locate element on driver.findElement(By.id("pin")).isDisplayed()) no matter what search mechanism I use. I have tried even xpath. basically I want the webdriver to wait until an element is present on screen and it does but even then I don't know why it gives error unable to locate the element error.

Banana
  • 2,435
  • 7
  • 34
  • 60

1 Answers1

0

If I have understood your Question correct, after all your steps when checking the condition within while() loop i.e.

driver.findElement(By.id("pin")).isDisplayed();

You are seeing a NoSuchElementException.

The Java Docs clearly mentions that NoSuchElementException is thrown by either WebDriver.findElement(By by) or WebElement.findElement(By by) which matches your case.

Reasons

There can be a lot many reasons to see NoSuchElementException. A couple of them are as follows :

  • The Locator Strategy which you have used i.e. id may not be identifying the exact element.
  • The element may not be within the Viewport.
  • When your script searches for the element, by that time the element may not have rendered in the HTML DOM

Solution

The solutions for the above mentioned reasons would be :


Update

You are seeing NoSuchElementException at the following line :

while (driver.findElement(By.id("pin")).isDisplayed());

From your code block it is inconclusive why you want to check driver.findElement(By.id("pin")).isDisplayed() in a while loop. Once you do driver.findElement(By.id("button")).click(); you are redirected to a new page with a new HTML DOM. So on the new page if you want driver.findElement(By.id("pin")).isDisplayed() to be successful you have to induce WebDriverWait for the WebElement to be displayed as per my Answer's Solution#3.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks man for such an explaination. it was really helpful for me . i just want to add one thing here: i feel the webdriver is able to locate the element beucase it the execution is paused .. whta i am doing after that is . i am manually enter the PIN . and clicking continue and then the element (pin field) is gone and my execution resumes and starts checking other stuffs.. only problem i have is .. even the execution is successfully resumed . it gives me NSE for that specific step. i hop ei wa sable to expain myself . it would be helpful if you can help me here. – Abhimanyu Singh Jan 25 '18 at 11:02
  • I have updated my Answer. Factually, even though `the webdriver is able to locate the element` still **driver.findElement(By.id("pin")).isDisplayed()** can be unsuccessfull. See [ExpectedCondition](http://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html) documentation. If my **`Answer`** have catered to your **`Question`** please **`Accept`** the **`Answer`** by clicking on the tick mark beside my **`Answer`** just below the **`VoteDown`** arrow so the tick mark turns **`Green`**. – undetected Selenium Jan 25 '18 at 11:44