Search code examples
javaseleniumhtmlunithtmlunit-driver

Java - Get error when trying to login to a website with selenium webdriver (htmlunit)


I tried the below code with actual credentials for Indeed.com, and I get an error: Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate a node using //*[@id="signin_email"]

I get a similar error when I use By.id instead of By.xpath, any idea what's going on?

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;


public class Testing {

public static void main(String[] args) {

                WebDriver driver = new HtmlUnitDriver();

                driver.get("https://secure.indeed.com/account/login?service=my&hl=en_CA&co=CA");
                driver.findElement(By.xpath("//*[@id=\"signin_email\"]")).sendKeys("[email protected]");
                driver.findElement(By.xpath("//*[@id=\"signin_password\"]")).sendKeys("needHelp");
                driver.findElement(By.xpath("//*[@id=\"loginform\"]/button")).click();  
                driver.quit(); 

 } 
}

Solution

  • You need to enable java script, see the updated code.

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.htmlunit.HtmlUnitDriver;
    
    
    public class Testing {
    
    public static void main(String[] args) {
    
                    HtmlUnitDriver driver = new HtmlUnitDriver();
                    driver.setJavascriptEnabled(true);
                    driver.get("https://secure.indeed.com/account/login?service=my&hl=en_CA&co=CA");
                    driver.findElement(By.xpath("//*[@id=\"signin_email\"]")).sendKeys("[email protected]");
                    driver.findElement(By.xpath("//*[@id=\"signin_password\"]")).sendKeys("needHelp");
                    driver.findElement(By.xpath("//*[@id=\"loginform\"]/button")).click();  
                    driver.quit(); 
    
     } 
    }