You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium自动化登录谷歌遇‘此浏览器或应用可能不安全’错误的解决

解决Selenium自动化谷歌登录时的"This browser or app may not be secure"警告

一、警告触发的原因

谷歌账号安全检测系统会从多个维度识别非人工登录行为,你的代码触发警告主要有这些原因:

  • Selenium驱动的浏览器会携带webdriver这类特殊标识,直接被谷歌的机器人检测机制识别
  • 代码中启用了大量降低浏览器安全性的选项,比如--disable-web-security、--allow-running-insecure-content,这类设置会让浏览器被判定为"不安全环境"
  • 使用无痕模式(--incognito),无痕窗口的环境特征和普通用户日常使用的窗口差异明显
  • 操作过于机械:直接快速输入账号密码、无任何用户行为模拟(比如鼠标移动、页面滚动),不符合真实用户的操作节奏
  • 批量登录多个账号的行为,触发了谷歌的反滥用机制

二、具体解决方法

1. 清理不安全的Chrome启动选项

把代码中这些风险选项全部移除:

options.add_argument("--allow-running-insecure-content")
options.add_argument("--disable-web-security")
options.add_argument("--ignore-certificate-errors")
options.add_argument("--ignore-ssl-errors")
options.accept_insecure_certs = True
options.assume_untrusted_cert_issuer = True

这些选项不仅会触发安全警告,还会让浏览器暴露在安全风险中。

2. 使用undetected-chromedriver规避检测

这个库专门针对谷歌的机器人检测做了优化,替换原生的Selenium Chrome驱动:
首先安装库:

pip install undetected-chromedriver

然后修改代码中的驱动初始化部分:

import undetected_chromedriver as uc

# 替换原来的webdriver.Chrome初始化
driver = uc.Chrome(options=options)

3. 模拟真实用户行为

添加随机等待、鼠标移动等操作,避免机械性操作:

from selenium.webdriver.common.action_chains import ActionChains
import random

# 输入用户名前,先移动鼠标到输入框
actions = ActionChains(driver)
actions.move_to_element(email_input).perform()
# 随机等待0.5-2秒
sleep(random.uniform(0.5, 2))
email_input.send_keys(username)

# 点击下一步前也添加随机等待
sleep(random.uniform(0.5, 1.5))
next_button.click()

4. 使用普通用户配置文件而非无痕模式

移除--incognito选项,改为加载本地Chrome的用户数据目录,让浏览器拥有正常用户的Cookie和配置:

# 替换incognito选项,路径替换为你本地Chrome的用户数据目录
options.add_argument(r"user-data-dir=C:\Users\你的用户名\AppData\Local\Google\Chrome\User Data")
# 可以指定具体的配置文件(比如Profile 1)
options.add_argument(r"profile-directory=Profile 1")

5. 控制登录频率

如果是批量登录多个账号,每个账号登录之间添加足够长的随机等待(比如5-10分钟),或者使用不同的IP代理避免被识别为批量操作。

6. 启用应用密码(针对开启2FA的账号)

如果你的谷歌账号开启了两步验证,直接用密码登录会触发更多检测,建议在谷歌账号设置中生成应用密码,用这个密码代替原密码进行自动化登录。

三、保障浏览/应用安全的措施

  • 保留浏览器安全默认设置:不要禁用web安全、证书验证等核心安全功能,这些是浏览器抵御恶意网站的关键
  • 加密存储凭证:不要把账号密码明文存在CSV文件中,使用加密存储工具(比如cryptography库)对凭证进行加密,读取时再解密
  • 使用专用测试账号:不要用个人主账号进行自动化操作,创建专门的测试账号,并只分配必要的权限
  • 定期更新驱动和浏览器:保持Chrome浏览器和驱动版本完全一致,避免已知的安全漏洞
  • 限制脚本访问权限:自动化脚本不要随意共享,存储脚本的文件设置严格的访问权限,防止凭证泄露

附用户原始代码

from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
import csv
from selenium.webdriver.chrome.options import Options

with open('credentials.csv', 'r') as file:
    reader = csv.reader(file)
    next(reader) # skip header row
    for row in reader:
        username = row[0]
        password = row[1]
        
        
        user_agent= 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36'

        options = Options()
        options.accept_insecure_certs = True
        options.assume_untrusted_cert_issuer = True
        options.add_argument("--user-agent=%s" % user_agent)
        options.add_argument("--no-sandbox")
        options.add_argument("--allow-running-insecure-content")
        options.add_argument("--disable-impl-side-painting")
        options.add_argument("--disable-setuid-sandbox")
        options.add_argument("--disable-web-security")
        options.add_argument("--disable-seccomp-filter-sandbox")
        options.add_argument("--ignore-certificate-errors")
        options.add_argument("--disable-notifications")
        options.add_argument("--ignore-ssl-errors")
        options.accept_insecure_certs = True
        options.add_argument("--incognito")
        # executable_path = r'C:\Users\mdmsd2\Desktop\MEGAsync\Selenium_youtube\chromedriver.exe'
        driver = webdriver.Chrome(options=options)
        driver.get('https://accounts.google.com/signin/v2/identifier')

        email_input = driver.find_element(By.ID,"identifierId")
        email_input.send_keys(username)
        next_button = driver.find_element(By.ID, 'identifierNext')
        next_button.click()
        
        driver.implicitly_wait(10)
        password_input = driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div[2]/div/c-wiz/div/div[2]/div/div[1]/div/form/span/section[2]/div/div/div[1]/div[1]/div/div/div/div/div[1]/div/div[1]/input')
        password_input.send_keys(password)
        submit_button = driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div[2]/div/c-wiz/div/div[2]/div/div[2]/div/div[1]/div/div/button/span')
        submit_button.click()

        driver.implicitly_wait(10) # wait for page to load
        if driver.current_url == 'https://mail.google.com/mail/u/0/#inbox':
            print(f'Logged in successfully as {username}')
            with open('successful_logins.csv', 'a', newline='') as file:
                writer = csv.writer(file)
                writer.writerow([username])
        else:
            print(f'Failed to login as {username}')

        driver.maximize_window()

内容的提问来源于stack exchange,提问作者Masud Al Nahid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 23:05:09