18

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.

nopassport1
  • 1,821
  • 1
  • 25
  • 53
Balachander
  • 430
  • 1
  • 3
  • 15
  • As a workaround you can try https://www.zeolearn.com/magazine/sending-and-receiving-emails-using-nodejs – Rahul L Aug 22 '19 at 07:34
  • @Balachander I know the only python can I write code for you in python? – Hamza Lachi Aug 22 '19 at 09:24
  • Sure @HamzaLachi you can. I will see the logic and will try to implement it. – Balachander Aug 22 '19 at 09:45
  • Appreciate it. But I see you have deleted your original answer. It was not the right solution for me but it might work out for some one else reading this post. – Balachander Aug 22 '19 at 09:56
  • Bro, I write that script that login and type password Do you know How To Start python script? – Hamza Lachi Aug 22 '19 at 10:09
  • Use this patch for the ChromeDriver and it will log in fine: https://github.com/ultrafunkamsterdam/undetected-chromedriver – shivams May 21 '21 at 13:45
  • Are you using an email id that is like xxxx@gmail.com ? It is observed that using a business email id that is issued by GSuit works well. Like xxx@business-name.com – Smit Sarang Aug 21 '21 at 15:57

8 Answers8

7

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!

Dharman
  • 30,962
  • 25
  • 85
  • 135
Balachander
  • 430
  • 1
  • 3
  • 15
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
  • 2
    Can you share your complete driver options config please – Kumar Sampath Mar 17 '20 at 03:16
  • @KumarSampath I later realized that the settings actually didn't help me. I've edited my answer to include what I have consistently observed to work. – Yinka Alabi Mar 17 '20 at 10:27
  • That does not work anymore: `InvalidArgumentException: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir` – Zhivko.Kostadinov Dec 22 '21 at 13:32
1

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.

alkakaushal
  • 144
  • 1
  • 5
0

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()
Hamza Lachi
  • 1,046
  • 7
  • 25
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/198323/discussion-on-answer-by-hamza-lachi-gmail-is-blocking-login-via-automation-sele). – Andy Aug 22 '19 at 18:10
  • 1
    Please add some explanation to your answer such that others can learn from it. What does all this code do? – Nico Haase Apr 12 '20 at 12:31
0

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()
  • Welcome to Stack Overflow! Please take the [tour](https://stackoverflow.com/tour). Thanks for contributing an answer. May you add an explanation on how your code solves the problem? – Saeid Amini Dec 16 '20 at 07:53
0

Found solution, add these arguements:

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

^^ C# btw

0

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

maximus
  • 1,290
  • 1
  • 14
  • 18
0

Below code worked for me 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")