0

I tried to login to gmail using headless browser with Selenium but it throws :

'Unable to locate element: {"method":"xpath","selector":"//input[@id='identifierId']"} 

But the same is working perfect with normal browser run using selenium. My code snippet below:

public static WebDriver login_page() throws InterruptedException {

    //WebDriver driver = new HtmlUnitDriver();
    System.setProperty("webdriver.chrome.driver", "D:/Selenium/chromedriver.exe");
    ChromeOptions option=new ChromeOptions();
    option.addArguments("start-maximized");
    option.addArguments("disable-infobars");
    option.addArguments("--disable-extensions");
    option.addArguments("--headless");
    WebDriver driver = new ChromeDriver(option);
    driver.get("https://www.gmail.com");       
    Thread.sleep(10000);
    System.out.println("Page title is: " + driver.getTitle()); 
    String title = driver.getTitle();
    try {
        Assert.assertEquals(title, "Gmail");
        System.out.println("Correct Page Loaded");
    }catch(AssertionError e) {
        System.out.println("Correct Page NOT Loaded");
    }
    WebElement name = driver.findElement(By.xpath("//input[@id='identifierId']"));
    name.sendKeys("test@gmail.com");
    driver.findElement(By.xpath("//span[@class='RveJvd snByac'][contains(text(),'Next')]")).click();
    Thread.sleep(5000);
    WebElement pwd= driver.findElement(By.xpath("//input[@type='password']"));
    pwd.sendKeys("Passwor1234");
    driver.findElement(By.xpath("//span[@class='RveJvd snByac'][contains(text(),'Next')]")).click();
    // System.out.println("Page title is: " + driver.getTitle());
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
sebin john
  • 23
  • 1
  • 2
  • 11
  • Possible duplicate of [Selenium test scripts to login into google account through new ajax login form](https://stackoverflow.com/questions/45953043/selenium-test-scripts-to-login-into-google-account-through-new-ajax-login-form) – undetected Selenium May 11 '18 at 11:37

1 Answers1

0

Please have a look on this detailed xpath document here http://chercher.tech/java/relative-xpath-selenium-webdriver

your answer might be here in "Normalize Space in Xpath"

sample usage driver.findElement(By.xpath("//button[normalize-space (contains(.,'" + Hello World+ "'))]")).click();

hope this will work

Best Regards, Gaurav