Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: //*[@id='login-email']

10,369

Solution 1

As you access the url https://staging.keela.co/login there is a Ajax loader which blocks the UI, so we have to wait for the Ajax loader to complete loading the all the WebElements and the email and password field becomes visible. To achieve that we will introduce ExplicitWait i.e. WebDriverWait with ExpectedConditions set to elementToBeClickable for the email field.Here is the working code block:

System.setProperty("webdriver.gecko.driver","C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://staging.keela.co/login");
WebDriverWait wait = new WebDriverWait (driver, 15);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='login-email']")));
element.sendKeys("[email protected]");
driver.findElement(By.xpath("//input[@id='login-password']")).sendKeys("keela");
driver.findElement(By.xpath("//button[@class='btn btn-sm btn-block btn-primary']")).click(); 

Solution 2

Try this below code.

Note: If id attribute is available then you should use id and for xpath try to use relative xpath.

I have used explicit wait method, so your driver may able to find the next webelement, after page is fully loaded.

driver.get("https://staging.keela.co/login");
driver.manage().window().maximize();

//Explicit wait for 60 seconds, to find the webelement. You can increase or decrease the time as per your specification.        
new WebDriverWait(driver, 60).until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("login-email"))));
driver.findElement(By.id("login-email")).sendKeys("[email protected]");
driver.findElement(By.id("login-password")).sendKeys("keela");
driver.findElement(By.xpath("//button[@type='submit'][text()='Log in']")).click();
Share:
10,369
Bandana Singh
Author by

Bandana Singh

Updated on June 04, 2022

Comments

  • Bandana Singh
    Bandana Singh almost 2 years

    I had to re-test the xpath, Previously it was working fine, But now it gives me an error.

    I tried with different locators as well, Like id, name. but still get the same error.

    package staging;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class login {
    
        public static void main (String[]args){
            System.setProperty("webdriver.gecko.driver","C:\\Program Files\\geckodriver.exe");
            WebDriver driver = new FirefoxDriver();
    
            //opening the browser
            driver.get("https://staging.keela.co/login");
    
            //logging
            driver.findElement(By.xpath("//*[@id='login-email']")).sendKeys("[email protected]");
            driver.findElement(By.xpath("//*[@id='login-password']")).sendKeys("keela");
            driver.findElement(By.xpath("//*[@id='login-form']/div[3]/div/button")).click();       
     }
    }