Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sign in to gmail account fails (selenium automation)

I have a Selenium service that has to login to my gmail account as the first step. This functionality was working couple of weeks ago, but suddenly the login starts to fails and i am seeing this Error in browser, tried both in Chrome and Firefox drivers in selenium -

enter image description here

This Error comes after the selenium service inserts the email,password and clicks on the sign in button. A similar error was also reported in Google support Forum here- https://support.google.com/accounts/thread/10916318?hl=en, They said that "Google seems to have introduced automation tools detection on their login flow!" but there is no solution in this thread.

Some Other Details which might be useful-

  • I am not able to login manually to Google accounts in the browsers
    opened by Selenium.
  • But I am able to login manually to these accounts in the Google Chrome application.

Let me know if you need to take a look at the code, i will post it here. Thanks in Advance!

Edit Adding Sample code to refer.

public void loginGoogleAccount(String emailId, String password) throws Exception {
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--profile-directory=Default");
    options.addArguments("--whitelisted-ips");
    options.addArguments("--start-maximized");
    options.addArguments("--disable-extensions");
    options.addArguments("--disable-plugins-discovery");
    WebDriver webDriver = new ChromeDriver(options);
    webDriver.navigate().to("https://accounts.google.com");
    Thread.sleep(3000);
    try {
        WebElement email = webDriver.findElement(By.xpath("//input[@type='email']"));
        email.sendKeys(emailId);
        Thread.sleep(1000);

        WebElement emailNext = webDriver.findElement(By.id("identifierNext"));
        emailNext.click();
        Thread.sleep(1000);

        WebDriverWait wait = new WebDriverWait(webDriver, 60);
        wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("identifierNext")));

        Thread.sleep(3000);
        WebElement passwordElement = webDriver.findElement(By.xpath("//input[@type='password']"));
        passwordElement.sendKeys(password);

        Thread.sleep(1000);
        WebElement passwordNext = webDriver.findElement(By.id("passwordNext"));
        passwordNext.click();

    } catch (Exception e) {
        LOGGER.info(String.format("No email/password field available or it is already logged in: [%s]: ",
                e.getMessage()));
    }
}
like image 219
gashu Avatar asked Dec 30 '19 16:12

gashu


2 Answers

I just tried something out that worked for me after several hours of trial and error.

Adding args: ['--disable-web-security', '--user-data-dir', '--allow-running-insecure-content' ] to my config resolved the issue.

I realized later that this was not what helped me out as I tried with a different email and it didn't work. After some observations, I figured something else out and this has been tried and tested.

Using automation:

Go to https://stackoverflow.com/users/login Select Log in with Google Strategy Enter Google username and password Login to Stackoverflow Go to https://gmail.com (or whatever Google app you want to access)

After doing this consistently for like a whole day (about 24 hours), try automating your login directly to gmail (or whatever Google app you want to access) directly... I've had at least two other people do this with success. PS - You might want to continue with the stackoverflow login until you at least get a captcha request as we all went through that phase as well.

like image 194
Yinka Alabi Avatar answered Oct 22 '22 21:10

Yinka Alabi


This issue was because of the selenium chrome profile. Create a new chrome profile and logged into it with the email id with which you were facing the issue. Then Turn on sync.

With this chrome profile in place I can skip the login steps and directly do the main process. Use: Chrome Options to add newly created chrome profile as an argument.

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary("Binary path of the Chrome");

Hope this one helps you.

like image 20
pramod_maddu Avatar answered Oct 22 '22 21:10

pramod_maddu