这是我登录 Google 的 python 代码
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
在非无头模式下,一切正常。在根据屏幕截图发送邮件后的无头模式下,我收到浏览器不安全的消息。如上所述,使用代理的解决方案没有帮助。我还尝试了在没有无头但没有无头模式的情况下使用谷歌登录后提出的解决方案,但 没有成功。还有其他建议吗?