29

I've tried login with Gmail or any Google services but it shows the following "This browser or app may not be secure" message:

error image

I also tried to do options like enable less secure app in my acc but it didn't work. then I made a new google account and it worked with me. but not with my old acc.

  1. how can i solve this ?
  2. How can i open selenium in the normal chrome browser not the one controlled by automated software ?

    This is my code
    from selenium.webdriver import Chrome
    from selenium.webdriver.chrome.options import Options


    browser = webdriver.Chrome()
    browser.get('https://accounts.google.com/servicelogin')
    search_form = browser.find_element_by_id("identifierId")
    search_form.send_keys('mygmail')
    nextButton = browser.find_elements_by_xpath('//*[@id ="identifierNext"]') 
    search_form.send_keys('password')
    nextButton[0].click() 
DarthQuack
  • 1,254
  • 3
  • 12
  • 22
Abdulrahman Dawoud
  • 411
  • 1
  • 4
  • 7

10 Answers10

32

First of all don't use chrome and chromedriver. You need to use Firefox.(if not installed) Download and install Firefox. Login to Google with normal Firefox.

You need to show the Google site that you are not a robot. You can use code like this:

from selenium import webdriver
import geckodriver_autoinstaller
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

geckodriver_autoinstaller.install()

profile = webdriver.FirefoxProfile(
    '/Users/<user name>/Library/Application Support/Firefox/Profiles/xxxxx.default-release')

profile.set_preference("dom.webdriver.enabled", False)
profile.set_preference('useAutomationExtension', False)
profile.update_preferences()
desired = DesiredCapabilities.FIREFOX

driver = webdriver.Firefox(firefox_profile=profile,
                           desired_capabilities=desired)

This can help you find your profile location.

But, why Firefox?

Actually there is only one reason, chromedriver was coded by Google. They can easily understand if it is a bot or not. But when we add user data with Firefox, they cannot understand if there is a bot or not.

You can fool Google like this. It worked for me too. I tried very hard to do this. Hope it will be resolved in you too.

yusufusta
  • 399
  • 3
  • 7
  • Consider elaborating on why Chrome with Chromedriver doesn't work and why the Firefox method does. – Marsroverr Feb 22 '21 at 01:33
  • @MLarionov Obviously it's very simple. Chrome was coded by Google. They can easily understand if it is a bot or not. But Firefox is different. It looks like a real browser when we use user data. – yusufusta Feb 23 '21 at 00:34
  • 1
    (+1) I just added ```driver = webdriver.Firefox(firefox_profile=path)``` and it worked! I could find my profile path as described [here](https://support.mozilla.org/en-US/kb/profiles-where-firefox-stores-user-data). – Shub Sep 04 '21 at 16:03
  • It's working on mac in 2021! – Aaditya Ura Nov 23 '21 at 05:04
  • My profile is about 1GB on disk and when you specify a profile, the selenium code COPIES the entire profile when you create your driver. To solve this, create a new empty profile for automation, and open a browser with it once and use it to authenticate via google signin, then point selenium to THIS profile. – snoopyjc Dec 29 '21 at 11:13
  • 8
    Not working anymore, 2022 – Ahmet Aziz Beşli Mar 09 '22 at 21:44
  • Works fine for me on an M1 Mac (May 2022) - it works even if the profile is in use in another browser instance, though you probably should have a profile setup for automated actions. – Josh May 26 '22 at 22:04
  • @AhmetAzizBeşli Is there no solution for 2022? – Memmo Jun 22 '22 at 12:59
  • python on windows I got errors with the URL and the message ```DeprecationWarning: firefox_profile has been deprecated, please use an Options object``` – mdkb Feb 06 '23 at 05:58
21

This is working for me. I found the solution from GitHub.

   from selenium import webdriver
   from selenium_stealth import stealth

   options = webdriver.ChromeOptions()
   options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36")
   options.add_experimental_option("excludeSwitches", ["enable-automation"])
   options.add_experimental_option('useAutomationExtension', False)
   options.add_argument('--disable-blink-features=AutomationControlled')
   driver = webdriver.Chrome(options=options)
   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://www.google.com")
Praveen Kumar
  • 849
  • 8
  • 8
8

You can easily bypass the google bot detection with the undetected_chromedriver:

pip install undetected-chromedriver
pip install selenium

Credits: https://github.com/xtekky/google-login-bypass/blob/main/login.py

import undetected_chromedriver as uc
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class Main:
  def __init_(self) -> None:
    self.url    = 'https://accounts.google.com/ServiceLogin'
    self.driver = driver = uc.Chrome(use_subprocess=True)
    self.time   = 10
    
  def login(self, email, password):
    # edit: fixed missing end-quotes on below lines
    WebDriverWait(self.driver, 20).until(EC.visibility_of_element_located((By.NAME, 'identifier'))).send_keys(f'{email}\n')
    WebDriverWait(self.driver, 20).until(EC.visibility_of_element_located((By.NAME, 'password'))).send_keys(f'{password}\n')
                                                                                
    self.code()
                                                                                  
  def code(self):
    # [ ---------- paste your code here ---------- ]
    time.sleep(self.time)                                                                                  
                                                                                  
if __name__ == "__main__":
  #  ---------- EDIT ----------
  email = 'email' # replace email
  password = 'password' # replace password
  #  ---------- EDIT ----------                                                                                                                                                         
 
  driver = Main()
  driver.login(email, password) 
Community
  • 1
  • 1
xtekky
  • 572
  • 1
  • 4
  • 13
3

From terminal pip install undetected-chromedriver then do the following steps, as shown below.
NOTE: indent your code inside if name == main, as i have done, only then the program will work

import undetected_chromedriver as uc
from time import sleep
from selenium.webdriver.common.by import By


if __name__ == '__main__':
    
    driver = uc.Chrome()
    driver.get('https://accounts.google.com/')

    # add email
    driver.find_element(By.XPATH, '//*[@id="identifierId"]').send_keys(YOUR EMAIL)
    driver.find_element(By.XPATH, '//*[@id="identifierNext"]/div/button/span').click()
    sleep(3)
    driver.find_element(By.XPATH, '//*[@id="password"]/div[1]/div/div[1]/input').send_keys(YOUR PASSWORD)
    driver.find_element(By.XPATH, '//*[@id="passwordNext"]/div/button/span').click()
    sleep(10)


  • Only this worked after 2 days of effort with no other solution. Thank you @Salim-muneer-lala. – Ragova Apr 23 '22 at 19:33
  • Pleasure @Ragova – Salim muneer lala Nov 23 '22 at 10:24
  • after trying the firefox method and chrome and running into issues because of firefox_profile being deprecated, this is the only thing that worked, but I had to increase the sleep(3) to sleep(10) between username entry and pwd entry to give it more time. – mdkb Feb 06 '23 at 06:46
  • EDIT: correction to previous comment. this only worked the first time, after that I am getting "This browser or app may not be secure." again after the username is entered. – mdkb Feb 06 '23 at 06:58
  • When I used VPN to change ip address it worked to let me log into google but also requested a 2FA for added security due to suspicious activity. So as of Feb 2023 I think this method is not working without additional measures like changing IP address between attempts to use "undetected_chromedriver" – mdkb Feb 06 '23 at 07:11
2

Firstly, I would ask you to use the undetected_chromedriver module

I have found some issues with the original repo so I have forked a repo that supports user_data_dir and also allows you to provide custom chromedriver path as well.

Uninstall the old module and install this by using git clone and then go the folder and run python setup.py install

Forked repo link : https://github.com/anilabhadatta/undetected-chromedriver

Import the latest undetected_chromdriver module:

import undetected_chromedriver.v2 as ucdriver

For using user_data_dir feature write:

options.user_data_dir = "path_to _user-data-dir"

instead of using this

options.add_argument(f"user_data_dir={path_to _user-data-dir}")

profile_directory name is the same as how we write in selenium

options.add_argument(f"--profile-directory=Default")

For using custom chrome path,

options.binary_location = chrome_path

For using custom chromedriver path,

driver = ucdriver.Chrome(executable_path=f'{path_to_chromedriver}', options=options)

Recently Google made some changes for which the authentication part didn't work.

I have tested this in Python 3.9.0, there are reports that it may not work correctly in 3.10.0

And this is tested in both Windows and Linux.

Final Code:

def load_chrome_driver(headless):
    chrome_loc = "/home/ubuntu/Downloads/chromium-browser/"
    chrome_path = chrome_loc + "chrome"
    chromedriver_path = chrome_loc + "chromedriver"
    user_data_dir = "/home/ubuntu/.config/chromium/custom_user"
    options = webdriver.ChromeOptions()
    if headless:
        options.add_argument('headless')
    options.add_argument('--profile-directory=Default')
    options.add_argument("--start-maximized")
    options.add_argument('--disable-gpu')
    options.add_argument('--no-sandbox')
    options.add_argument("--disable-dev-shm-usage")
    options.add_argument('--log-level=3')
    options.binary_location = chrome_path
    options.user_data_dir = user_data_dir
    driver = ucdriver.Chrome(
        executable_path=chromedriver_path, options=options)
    driver.set_window_size(1920, 1080)
    driver.set_window_position(0, 0)
    return driver
1

If you'd prefer Chrome over Firefox, the way to go around Googles automation detection is by using the undetected_chromedriver library. You can install the package using pip install undetected-chromedriver. Once your driver object is initiated you can simply use selenium to operate the driver afterwards.

# initiate the driver with undetetcted_chromedriver
import undetected_chromedriver.v2 as uc
driver = uc.Chrome()

# operate the driver as you would with selenium
driver.get('https://my-url.com') 

# Example use of selenium imports to be used with the driver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By

try:
    driver.find_element(By.XPATH, '//*[@id="my-id"]').click()
except NoSuchElementException:
    print("No Such Element Exception")

Note: the login of Google is a popup, so don't forget to swap window handles to the popup to login and then afterwards to switch back to the main window.

questioning
  • 245
  • 1
  • 4
  • 18
  • This does not work for me on MacOS. It raises a RuntimeError. – Alex Mar 05 '22 at 08:32
  • Please upvote this! I went on a wild goose chase trying many other suggestions here and on linked pages with Gecko and others, this was the only one that worked for me. And so simple. Thank you @questioning!! Win64 Chrome 115 – DLuber Aug 18 '23 at 22:11
0

You can use undetected-chromedriver from github

import undetected_chromedriver as uc
from selenium import webdriver

options = uc.ChromeOptions()
options.add_argument("--ignore-certificate-error")
options.add_argument("--ignore-ssl-errors")
# e.g. Chrome path in Mac =/Users/x/Library/xx/Chrome/Default/
options.add_argument( "--user-data-dir=<Your chrome profile>")
driver = uc.Chrome(options=options)
url='https://accounts.google.com/servicelogin'
driver.get(url)

Your first import need to be undetected Chrome driver.

Susil Parida
  • 382
  • 2
  • 4
0

None of the answers here seem to work. Clearly Google follow this thread and improve their security if any of the answers show signs of breaking in! The answer to the OP's question #1 is - you can't, or at least a public internet forum is not the place to look for a solution. Below is a simple demonstration of this using Windows, Edge, Excel, VBA and Selenium. The solutions here show how to use an already logged in profile with Selenium and Chrome.

Option Explicit
Sub sbGoogleLogin()
    Dim driver As ChromeDriver
    Set driver = New ChromeDriver
    Dim sURL As String
    sURL = "https://mail.google.com/mail/u/0/?pli=1#inbox"
    driver.Start ("edge")
    driver.get (sURL)
    sbDelay (100000)
    driver.FindElementByXPath("//input[@type='email']").SendKeys ("current, valid gmail address required here")
    sbDelay (100000)
    driver.FindElementByXPath("/html/body/div/div/div[2]/div/c-wiz/div/div[2]/div/div[2]/div/div/div/div/button").Click
    MsgBox "wait"
    driver.Quit
End Sub
Sub sbDelay(delay As Long): Dim i As Long: For i = 1 To delay:  DoEvents: Next i: End Sub
user10186832
  • 423
  • 1
  • 9
  • 17
0

You can bypass bot detection with SeleniumBase in uc mode.

First pip install seleniumbase. Then you can run this script with python:

from seleniumbase import SB

with SB(uc=True) as sb:
    sb.open("https://www.google.com/gmail/about/")
    sb.click('a[data-action="sign in"]')
    sb.type('input[type="email"]', "test123@gmail.com")
    sb.click('button:contains("Next")')
    sb.sleep(5)
    # sb.type('input[type="password"]', PASSWORD)
    # sb.click('button:contains("Next")')

To access the raw driver from the script, use sb.driver. See API Reference for SeleniumBase methods.

Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
-1

Google is detecting that you're using a bot. You must first of all add this snippet of config (convert it to python since i wrote it in java):

    options.addArguments("--no-sandbox");
            options.addArguments("--disable-dev-shm-usage");
            options.addArguments("--disable-blink-features");
            options.setExperimentalOption("excludeSwitches", Arrays.asList("enable-automation"));
            options.addArguments("--disable-blink-features=AutomationControlled");
            options.addArguments("--disable-infobars");

        options.addArguments("--remote-debugging-port=9222");

options.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);

driver.executeScript("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})");

The last instruction is the key one, when you launch Selenium, the navigator var of Chrome is set to 'true', that means that the browser is controlled by a bot, setting it with JS to undefined renders it a "normal browser" in view of Google. Another important thing you should do is to change the HEX of the chromedriver binary, i can PM you my already modified one (ubuntu environment), if you trust me obv.