'GMail is blocking login via Automation (Selenium)

I am using selenium to automate a mail verification process in a web application. I have a script already in place to login to gmail and read an activation mail received on the account. The script was perfectly working till yesterday but today I am facing a problem.

Screenshot of issue

Additional Screenshot of issue Additional Screenshot of issue

Gmail is not allowing sign in if the browser is launched with selenium. Says,

You're using a browser that Google doesn't recognize or that's setup in a way that we don't support.

  • I have tried upgrading chromedriver version to 76.0.0 as I am using chrome version 76.0.3809.100(64 bit). (Previously used chromedriver 2.45) Still, the problem persists.
  • Verified that this issue occurs even if I use Firefox instead of Chrome for automation.
  • Verified that Javascript is enabled in the browser
  • Gmail is not asking for any OTP or recovery mail. It is simply blocking my attempt to login via automation. However I am able to login to the same account manually.

Software used: "webdriverio": "^4.14.1", "wdio-cucumber-framework": "^2.2.8"

Any help is appreciated.



Solution 1:[1]

After some trial and error, found out that this issue happens only in a scenario when multiple gmail accounts have already been created from the same App/IP/Device. Google somehow is marking those accounts and blocks them if they are launched by automation frameworks/extensions.

Temporary Solutions:

  • Create a fresh GMail account using a different mobile number from another device (Not recommended).
  • We should be using workarounds like nodemailer zeolearn.com/magazine/sending-and-receiving-emails-using-nodejs (as mentioned by Rahul L as a suggestion)
  • Automate temporary mail providers like Guerilla Mail or 10 Minute Mail if you are worried about only receiving mails

My humble opinion is to entirely avoid automating the UI of third party Mail applications as you cannot predict how their UI and elements will change. They might block you from launching for security purposes and they have every right to do so!

Solution 2:[2]

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.

Solution 3:[3]

Go to Gmail --> Settings --> Search for "Less secure app access" --> Enable

This is not a suggested method because it allows access to less secure apps and it's easier to get into your account but if it's just a testing account and no important data is being transferred to or from, you can give this a try.

Solution 4:[4]

You need to open your editor and copy this code and paste them and save with this name as email.py and then open your terminal/cmd/powershell in that directory and type this python .\email.py

Note:

Make sure your chrome driver in that directory where you save python file

You need to copy this code and paste in your editor

Here is that script:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import pyautogui as pg


username = input("Enter Your Username: ")

password = input("Enter Your Password: ")

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1#identifier")
driver.maximize_window()


mail = WebDriverWait(driver, 100).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='identifierId']"))).send_keys(username)

login = WebDriverWait(driver, 100).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='identifierNext']/span"))).click()

passw = WebDriverWait(driver, 100).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='password']/div[1]/div/div[1]/input"))).send_keys(password)

next = WebDriverWait(driver, 100).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='passwordNext']/span/span"))).click()

Solution 5:[5]

Steps to login to gmail through stackoverflow :

  1. Open a browser window and open stackoverflow
  2. Click on log in
  3. Login with google
  4. Enter email and password
  5. Stackoverflow is logged in with gmail credentials
  6. Now open mail.google.com (gmail.com)
  7. You are now logged into gmail using selenium

You can copy this code and paste in your editor, you have to enter the path of your chrome driver, email address and password for your gmail where it is asked in the code.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time

driver=webdriver.Chrome('Enter the path of the chrome driver here')
driver.get("https://stackoverflow.com/")

driver.maximize_window()

time.sleep(5)

driver.find_element(By.XPATH, '/html/body/header/div/ol[2]/li[2]/a[1]').click()#Log in button in stackoverflow
time.sleep(5)

driver.find_element(By.XPATH, '//*[@id="openid-buttons"]/button[1]').click()# Log in with Google button
time.sleep(5)

driver.find_element(By.XPATH, '//*[@id="identifierId"]').send_keys("Enter email address")# Enter email address

time.sleep(5)

driver.find_element(By.XPATH, '//*[@id="identifierNext"]/div/button/div[2]').click() # Click next button after entering email address
time.sleep(5)

driver.find_element(By.XPATH, '//*[@id="password"]/div[1]/div/div[1]/input').send_keys("Enter password")#Enter password

time.sleep(5)

driver.find_element(By.XPATH, '//*[@id="passwordNext"]/div/button/div[2]').click()# Click on next button after entering the password

time.sleep(5)

driver.get("https://mail.google.com")

time.sleep(5)

driver.close()

Solution 6:[6]

Found solution, add these arguements:

options.AddArguments("--disable-web-security", "--user-data-dir=true", "--allow-running-insecure-content");

^^ C# btw

Solution 7:[7]

I think this is not a problem with the automation framework nor with the automated Chrome application, it is Google server that blocks automated login requests. I don't have any clear evidence/really understand how they can do it, but several clues point me to this direction:

  • First, on the login page, after entering your user name and clicking next, a user lookup request is fired, something likes https://accounts.google.com/_/lookup/accountlookup?hl=vi&_reqid=xxxxx&rt=j, the response of this request includes something likes https://accounts.google.com/signin/rejected?rrk=46&hl=vi. In subsequent requests, the browser just tries to resolve & display what the error means to the user.
  • Second, it was stated here that "Google doesn’t let you sign in from some browsers. Google might stop sign-ins from browsers that are being controlled through software automation rather than a human". This means Googles has implemented measures to detect requests coming automated browser, especially Chrome-family browsers in debug mode, which are used by many automation frameworks.
  • A thread about similar issue with Taiko also mentioned that Google blocks requests from Chrome running in debug mode.

Recently I tried with Cypress and Taiko, but none works, same "rejected" issue, which ruined my initial plan of doing an e2e test for my app (which uses GG OIDC login).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharman
Solution 2
Solution 3 alkakaushal
Solution 4
Solution 5
Solution 6 CashMoneyPat
Solution 7 maximus