Exception in Thread "Main" Org.Openqa.Selenium.Nosuchelementexception: Unable to Locate Element: //*[@Id='Login-Email']

Selenium: Element Not Found for existing element

It should be By.className instead of cssSelector.

Example:

    WebElement parentElement = driver.findElement(By.className("rose-button"));

If you want to use cssSelector, it should look like:

driver.findElement(By.cssSelector("button[class='app-button rose-button min-width']"));

Another way is using xpath:

driver.findElement(By.xpath("//div[@class='app-button rose-button min-width']"));

And as DebanjanB mentioned, you can add webdriverwait also, if there is some kind of loading delay.

How to click a button in an automated test using Selenium and Java

To invoke click() on the element you can use either of the following Locator Strategies:

  • Using linkText:

    driver.findElement(By.linkText("Login")).click();
  • Using cssSelector:

    driver.findElement(By.cssSelector("a[href='/login']")).click();
  • Using xpath:

    driver.findElement(By.xpath("//a[@href='/login' and text()='Login']")).click();

Best practices

As you are invoking click() ideally you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • Using linkText:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Login"))).click();
  • Using cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[href='/login']"))).click();
  • Using xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@href='/login' and text()='Login']"))).click();

Reference

You can find a couple of relevant discussions in:

  • NoSuchElementException, Selenium unable to locate element
  • Exception in thread “main” org.openqa.selenium.NoSuchElementException: Unable to locate element: //*[@id='login-email']


Related Topics



Leave a reply



Submit