Sign in to gmail account fails (selenium automation)

倖福魔咒の 提交于 2020-02-13 22:26:59

问题


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 -

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()));
    }
}

回答1:


Toggle "Allow Less Secure App Access"
There is a setting on your account that you can toggle that may help with this. It is the "Allow Less Secure App Access". You should be able to visit the link below to toggle that setting if you are already logged into the gmail account you want to modify.

Link to change setting on google account: https://myaccount.google.com/lesssecureapps

Further information(source): https://support.google.com/accounts/answer/6010255




回答2:


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.




回答3:


Check if your Chrome version is >= 79 and if so, downgrade to 78. Here's what I did (Windows):

I uninstalled "retail Chrome" which constantly kept upgrading itself to the latest version using sneaky tricks such as Google Chrome services that would check for updates in the background.

Even disabling those system services didn't help because retail Chrome also installs timer events that would re-enable said services in the middle of the night, so you'd wake up to a new version, and not even notice until things break.

I installed v78 from the "offline installer" found here, which doesn't seem to install any "helpful" auto upgrade features: https://www.neowin.net/news/google-chrome-780390470-offline-installer/

The above problem went away like magic. It appears that v79 has some anti-feature built in that calls home with information that allows Google to conclude that a bot is at work.

Hope this works for you... if not, you could invest a lot of time and create your own "Chrome simulator" by patching and compiling Chromium accordingly...



来源:https://stackoverflow.com/questions/59534028/sign-in-to-gmail-account-fails-selenium-automation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!