6

After a lot of Google searching, I got my python script to login with Selenium, but now the page is telling me that:

This browser or app may not be secure. Learn more
Try using a different browser. If you’re already using a supported browser, you can refresh your screen and try again to sign in.

Is there anyway around this? Here is my current script.

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys


driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://accounts.google.com/signin/v2/identifier")
email = driver.find_element_by_id('identifierId')
email.click()
email.send_keys(username)
email.send_keys(Keys.ENTER)

actions = ActionChains(driver)
actions.send_keys(password+Keys.ENTER)
actions.perform()

Sending keys directly to the password field wasn't working so I tried sending keys to the browser itself and now I'm getting this error. I don't know how to proceed further.

Edit: Toggling Less secure app access did not work: https://stackoverflow.com/a/59607923/11343425

4
  • 2
    Why are you logging into Gmail at all? Can't you use their API?
    – pavelsaman
    Jan 25, 2020 at 7:24
  • 1
    Your code works fine (just add a sleep before sending to password field ) i am not getting any error .could you put the full error and screen shots
    – PDHide
    Jan 26, 2020 at 1:32
  • @PDHide - "This browser or app may not be secure. Learn more Try using a different browser. If you’re already using a supported browser, you can refresh your screen and try again to sign in." Jan 26, 2020 at 13:57
  • 2
    This works pypi.org/project/selenium-stealth Dec 4, 2021 at 2:48

3 Answers 3

6

This Google Support page states that sign in via browsers that "Use automation testing frameworks" is being disabled for the following security reasons and Google advices to do "Sign in with Google" using browser-based OAuth 2.0 authentication service.

As some websites, like stackoverflow.com allow you to sign in to their services using "Sign in with Google" it must happen via Google OAuth 2.0 authentication. This implicates that doing so you are also indirectly signing in to your Google account and therefore you can use all the Google services.

So you can fully automatically sign in to your Google account, e.g. by using a Python script, by performing these actions in your code:

  1. Open a new browser window that is controlled by selenium webdriver
  2. In the same window load the StackOverflow login page (or any other site that uses "Sign in with Google")
  3. Choose for "Log in with Google"
  4. Provide your Google account credentials and login to StackOverflow
  5. Load the Google mailbox by opening https://mail.google.com/ or https://www.gmail.com/

This way you land down in your Gmail mailbox without performing any manual actions.

Please remember to add some 5s delays between different actions as doing it too quickly or too frequently can be recognized by StackOverflow as malicious automated actions and you can get blocked and you will need to make the manual I'm not a robot verification

6
  • 1
    I've moved on in my life since I asked this question in January, but I trust the community here. Have an accept. Jun 9, 2020 at 12:37
  • 1
    I've been trying to solve this for days. This the correct answer! Oct 25, 2020 at 20:05
  • 1
    Is this still working? Not for me. Error 'This browser or app may not be secure' appears Jul 12, 2021 at 17:21
  • 1
    Dosn't work for me
    – 4xy
    Aug 10, 2021 at 17:02
  • Yes. it has been working for me for the last year and it still works every day. I advise you to check first if you can manually login to your gmail account. If you have issues you could use --user-data-dir=<my-data-dir> config option for the chromedriver so you have session cookies set and then do login once manually. Next time when running the script opening the login page will redirect you automatically to your stack account page and then you can just switch to gmail.
    – Tony
    Aug 10, 2021 at 18:42
2

Google forbids using automated scripts for logging into Gmail.

To help protect your account, Google doesn’t let you sign in from some browsers. Google might stop sign-ins from browsers that:

  • Don’t support JavaScript or have Javascript turned off.
  • Have unsecure or unsupported extensions added.
  • Use automation testing frameworks.
  • Are embedded in a different application.

See: https://support.google.com/accounts/answer/7675428?hl=en

0
  1. Open browser which controlled by selenium
  2. Open StackOverflow site manually(or another site which have authorization with google)
  3. Perform authorization with google manually

Then when you run your code again with gmail authorization it should work fine.

1
  • doesn't work anymore Dec 4, 2021 at 1:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.