'Cannot login using Google Authentication in headless mode for undetected chromedriver in Python

This my python code to login into Google

from seleniumwire.undetected_chromedriver.v2 import Chrome, ChromeOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from pathlib import Path


    def execute_authorization(url, email, password):
        # Create empty profile
        Path("./chrome_profile").mkdir(parents=True, exist_ok=True)
        Path('./chrome_profile/First Run').touch()
        options = {}
        chrome_options = ChromeOptions()
        chrome_options.add_argument('--disable-gpu')
        chrome_options.add_argument('--incognito')
        chrome_options.add_argument('--disable-dev-shm-usage')
        chrome_options.add_argument('--user-data-dir=./chrome_profile/')
        user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.50 Safari/537.36'
        chrome_options.add_argument('user-agent={0}'.format(user_agent))
        chrome_options.add_argument('--headless')
    
        browser = Chrome(seleniumwire_options=options, options=chrome_options)
        wait = WebDriverWait(browser, 10)
        browser.execute_script("return navigator.userAgent")
        browser.get(url)
        wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="identifierId"]')))
        browser.find_element_by_xpath('//*[@id="identifierId"]').send_keys(email)
        wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="identifierNext"]/div/button')))
        browser.find_element_by_xpath('//*[@id="identifierNext"]/div/button').click()
        wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="password"]/div[1]/div/div[1]/input')))
        browser.find_element_by_xpath('//*[@id="password"]/div[1]/div/div[1]/input').send_keys(password)
        wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="passwordNext"]/div/button')))
        browser.find_element_by_xpath('//*[@id="passwordNext"]/div/button').click()
        wait_for_correct_current_url(wait)
        return browser.current_url

In non headless mode everything works fine. In headless mode after giving mail based on screenshot I got the message that browser is not safe. As above solution with agent did not help. I also tried solutions proposed in post google login working in without headless but not with headless mode with no success. Any other proposals ?



Solution 1:[1]

When headless mode is activated, the Navigator.Webdriver flag is set to true, which indicates that the browser is controlled by automation tools. The code below worked for me.

chrome_options.add_argument('--disable-blink-features=AutomationControlled')

This blog has some other options you could try. https://piprogramming.org/articles/How-to-make-Selenium-undetectable-and-stealth--7-Ways-to-hide-your-Bot-Automation-from-Detection-0000000017.html

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 infamous kid