'Automation Google login with python and selenium shows ""This browser or app may be not secure""
I've tried login with Gmail or any Google services but it shows the following "This browser or app may not be secure" message:
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.
- how can i solve this ?
- 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()
Solution 1:[1]
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.
Solution 2:[2]
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")
Solution 3:[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)
Solution 4:[4]
You can easily bypass the google bot detection with the undetected_chromedriver
:
pip install undetected_selenium
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
# ---------- EDIT ----------
email = 'email\n' # replace email
password = 'password\n' # replace password
# ---------- EDIT ----------
driver = uc.Chrome(use_subprocess=True)
wait = WebDriverWait(driver, 20)
url = 'https://accounts.google.com/ServiceLogin?service=accountsettings&continue=https://myaccount.google.com%3Futm_source%3Daccount-marketing-page%26utm_medium%3Dgo-to-account-button'
driver.get(url)
wait.until(EC.visibility_of_element_located((By.NAME, 'identifier'))).send_keys(email)
wait.until(EC.visibility_of_element_located((By.NAME, 'password'))).send_keys(password)
print("You're in!! enjoy")
Solution 5:[5]
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.
Solution 6:[6]
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.
Solution 7:[7]
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.
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 | |
Solution 2 | Praveen Kumar |
Solution 3 | Salim muneer lala |
Solution 4 | undetected Selenium |
Solution 5 | Susil Parida |
Solution 6 | |
Solution 7 | questioning |