11

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()));
    }
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
gashu
  • 863
  • 2
  • 10
  • 21
  • 1
    Sounds like a limitation with Google themselves, they probably don't want people using UI automation to access their services. I've seen this same issue pop up on StackOverflow several times lately and the issue is always the same. You'll probably have better luck using their API. – CEH Dec 30 '19 at 16:58
  • It seems like google knows that a robot might be trying to log in and they don't want that to happen like @Christine said... Try modifying the chromedriver like mentioned [here](https://stackoverflow.com/questions/33225947/can-a-website-detect-when-you-are-using-selenium-with-chromedriver?rq=1) so that they can't detect the robot (script). Also, while logging in, you must use random wait before pressing each key in the text-boxes, so that you don't see a captcha. Do not directly send keys to text-boxes. – Ali Sajjad Dec 30 '19 at 20:24
  • @Christine Which API are you referring? – gashu Dec 31 '19 at 11:11
  • @AliSajjad That didn't help, I tried both with modifying the chromedriver and also used random wait time. – gashu Dec 31 '19 at 11:12
  • Have you tried modifying userAgent that is used? Maybe Selenium introduces itself as Selenium, thus Google tries to block it. This might help -> https://stackoverflow.com/a/29966769/2838206 – klubi Jan 01 '20 at 20:00
  • @klubi that didn't help – gashu Jan 02 '20 at 07:36
  • I'm curious, can you share why you needed to sign-in to gmail through automation? What can you not achieve through the APIs that google provide you? – Mavi Domates Jan 08 '20 at 16:23
  • After login, i am using a google ads service which doesn't have any api's – gashu Jan 15 '20 at 09:36

6 Answers6

5

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

NANfan
  • 449
  • 3
  • 16
  • I definitely recommend this solutions, this setting really helps we try to login using java mail api or using maven configuration without ui intervention. – Amit Jain Jan 07 '20 at 18:55
  • 11
    I have already tried this, Unfortunately, this didn't fix the issue. – gashu Jan 15 '20 at 09:28
  • @gashu Can you walk me through exactly what you are trying to do with this test? There are some tools out there that you could try to further isolate or fix what is going on. One such tool is mentioned in this article. If you give me more specifics on how the test is setup, what action is taken, and what is asserted at the conclusion of the test it will help with pointing you in a direction that can help. https://medium.com/@levz0r/how-to-poll-a-gmail-inbox-in-cypress-io-a4286cfdb888 – NANfan Jan 15 '20 at 18:09
  • 2
    The question was specifically regarding Selenium, which the less success app setting has nothing to do with, because to Google, Selenium is not an app, and certainly not a supported one. Google can detect that the login is initiated from a browser using a webdriver, and this new block specifically targets that. There is currently no known solution. – Moonstone Apr 01 '20 at 20:35
3

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.

Yinka Alabi
  • 307
  • 2
  • 3
1

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.

pramod_maddu
  • 93
  • 1
  • 8
  • 2
    Thanks for reply! But I was looking for a solution where we don't have to create chrome Profile, with profile if i am not wrong selenium can open one browser at a time, right? I start getting errors like Chrome profile is already is in use. – gashu Jan 03 '20 at 07:05
1

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...

blitter
  • 347
  • 5
  • 21
  • I was wrong to think Chrome v78 doesn't install sneaky auto upgrade features. It simply replaces itself every so often with version 80+ on restart. The only way I have found to stop this behavior is to look at `C:\Users\USERNAME\AppData\Local\Google\Chrome\Application` before starting chrome. If there's a file `new_chrome.exe` and/or a folder named `80.*` (or higher by the time you read this), then it has fired its forced auto-update. Delete `new_chrome.exe`, then launch `chrome.exe`. It would be trivial to write a batch file that deletes `new_chrome.exe` before starting `chrome.exe`. – blitter Mar 21 '20 at 18:27
0

The only one solution which worked for me, just create fresh random google account, and that's it. Worked without any problems.

10101101
  • 193
  • 1
  • 13
0

I fixed it by using selenium-stealth module and editing chromedriver.exe.

from selenium.webdriver.chrome.options import Options
from selenium import webdriver 
import time
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium_stealth import stealth
chrome_options = Options()
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches",["enable-automation"])
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument('--disable-logging')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--disable-blink-features=AutomationControlled')
caps = DesiredCapabilities.CHROME
caps['goog:loggingPrefs'] = {'performance': 'ALL'}

# for editing chromedriver exe so that its not detected(run only once)
with open("chromedriver.exe","rb") as d:
    file_data=d.read()
file_data=file_data.replace(b"cdc_",b"tch_")
with open("chromedriver.exe","wb") as d:
    d.write(file_data)
driver = webdriver.Chrome('chromedriver.exe',chrome_options=chrome_options,desired_capabilities=caps)

# for injecting js to that tab so that browser not detected
stealth(driver,languages=["en-US", "en"],vendor="Google Inc.",platform="Win32",webgl_vendor="Intel Inc.",renderer="Intel Iris OpenGL Engine",fix_hairline=True,)
driver.get("https://accounts.google.com")
time.sleep(3)
driver.switch_to.active_element.send_keys("myemail@gmail.com\n")
time.sleep(3)
driver.switch_to.active_element.send_keys("mypassword\n")