3
votes

Google Login is not working in headless chrome selenium in Jenkins job.

However this is working when I am running on actual chrome.

dvr.get("https://accounts.google.com/signin/v2/identifier?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2Fh%2F141icwbpdm6lq%2F&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin");

dvr.findElement(By.id("identifierId")).click();

dvr.findElement(By.id("identifierId")).clear();

dvr.findElement(By.id("identifierId")).sendKeys("[email protected]");

dvr.findElement(By.xpath("//*[@id=\"identifierNext\"]/span")).click();

dvr.findElement(By.xpath("//*[@id='password']/div[1]/div/div[1]/input")).clear();

dvr.findElement(By.xpath("//*[@id='password']/div[1]/div/div[1]/input")).sendKeys("xyz");

dvr.findElement(By.xpath("//*[@id=\"passwordNext\"]/span/span")).click();

Following error is coming in headless mode.

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"identifierId"} (Session info: headless chrome=76.0.3809.132) (Driver info: chromedriver=2.38.552518 (183d19265345f54ce39cbb94cf81ba5f15905011),platform=Mac OS X 10.12.6 x86_64) (WARNING: The server did not provide >any stacktrace information) Command duration or timeout: 0 milliseconds For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03' System info: host: 'Homebells-MacBook-Pro-2.local', ip: 'fe80:0:0:0:ab:bf46:6f97:5e3c%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.12.6', >java.version: '1.8.0_144' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, >chrome: {chromedriverVersion: 2.38.552518 (183d19265345f5..., userDataDir: /var/folders/jq/rrf_qymx39s...}, cssSelectorsEnabled: true, databaseEnabled: >false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, >networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, >takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 76.0.3809.132, webStorageEnabled: true} Session ID: 61f1a8512d345d1abf7ca3b40e345602 *** Element info: {Using=id, value=identifierId}

below is the code for launching browser.

if (browser.equalsIgnoreCase("chrome") && OS == "Mac") {

        System.out.println("==============================================" );
        System.out.println("OS Detected : MAC , Browser Launched : Chrome" );
        System.out.println("==============================================" );

        System.setProperty("Webdriver.chrome.driver", "./lib/chromedriver");

        Boolean headlesschrome = true;

        if (headlesschrome==true) {

            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.addArguments("--headless");
            dvr = new ChromeDriver(chromeOptions);
            dvr.manage().window().fullscreen(); 

        }else if (headlesschrome==false)

        {
            dvr = new ChromeDriver();
            dvr.manage().window().fullscreen();
        }

    }
4
Did you happen to try grabbing screen captures using selenium.remote.Augmenter to get an idea of what is being displayed on the page? When I do that it shows a completely empty screen, so wondering if anyone else has seen the same...Z4-tier

4 Answers

1
votes

Solved google login issue by adding --user-agent flag with headless chrome.

--user-agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36"

Ref: Headless Chrome displays old Google sign-in when signing in with OpenID/OAuth

0
votes

Chrome headless browser won't necessarily have the same screen size set as the normal Chrome (I think it is 800 x 600, but I'm not sure).

Try adding the following options:

chromeOptions.addArguments("--start-maximized");
chromeOptions.addArguments("--window-size=1200,800");

Obviously, set the screen size you want in the place of 1200x800.

0
votes

I used to have the same problem, in headless used other google login page. I resolved it this way:

driver.get("https://accounts.google.com/signin/v2/identifier");

    WebElement emailField = driver.findElement(By.id("Email"));
    emailField.sendKeys(login);


    WebElement loginNext = driver.findElement(By.id("next"));
    loginNext.click();

    logger.info("Enter password");
    WebElement passwordField = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Passwd")));
    passwordField.sendKeys(password);

    WebElement passwordNext = driver.findElement(By.id("signIn"));
    passwordNext.click();
0
votes

Error comes from your selectors, Driver is not able to find element, and that's why it is throwing an error.

Here is the old example of mine to login to the google account.

By usernameInput = By.xpath("//input[@type='email']");
By passwordInput = By.xpath("//input[@type='password']");
By nextButton = By.xpath("//span[contains(text(),'Next')]");


try {
        driver.get("https://accounts.google.com/signin/v2/identifier?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2Fh%2F141icwbpdm6lq%2F&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin");
        driver.findElement(usernameinput).sendkeys("fill it");
        driver.findElement(nextButton).click();
        Thread.sleep(2000);

        driver.findElement(passwordInput).sendKeys("fill it");
        driver.findElement(nextButton).click();
        Thread.sleep(2000);

} catch (Exception e) {

}