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

使用Selenium WebDriver和Python登录Google遭拦截问题求助

解决Selenium自动登录Google被检测的问题

Google的反自动化机制会识别Selenium驱动的特征,导致弹出“无法登录,此浏览器或应用可能不安全”的提示,以下是几种可行的解决方法:

1. 配置ChromeOptions隐藏自动化特征

通过修改Chrome启动参数,隐藏webdriver标识,避免被检测:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time

options = Options()
# 禁用自动化控制检测
options.add_argument("--disable-blink-features=AutomationControlled")
# 排除自动化启动标识
options.add_experimental_option("excludeSwitches", ["enable-automation"])
# 禁用自动化扩展
options.add_experimental_option('useAutomationExtension', False)
# 设置自定义User-Agent(匹配真实浏览器,可选)
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36")

driver = webdriver.Chrome(options=options)
# 篡改navigator.webdriver属性
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")

# 登录流程
driver.get("https://accounts.google.com/signin")
username = driver.find_element(By.NAME, "identifier")
username.send_keys("blabla@gmail.com")
username.send_keys(Keys.RETURN)
time.sleep(2)  # 替换过长sleep,建议后续改用显式等待

password = driver.find_element(By.NAME, "password")
password.send_keys("asdf1234")
password.send_keys(Keys.RETURN)

# 搜索流程
driver.implicitly_wait(5)
driver.get("https://www.google.com")
search_field = driver.find_element(By.NAME, "q")
search_field.send_keys("pizza")
search_field.send_keys(Keys.RETURN)

2. 正确使用Chrome用户数据目录

如果之前尝试配置文件失败,可能是路径错误或权限问题。确保使用已登录Google的Chrome用户数据路径:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
# Windows路径示例(替换为你的实际路径)
options.add_argument(r"user-data-dir=C:\Users\你的用户名\AppData\Local\Google\Chrome\User Data")
# Linux/macOS路径示例
# options.add_argument("user-data-dir=/home/你的用户名/.config/google-chrome/")
# 指定具体profile(默认是Default,可选)
options.add_argument("profile-directory=Default")

driver = webdriver.Chrome(options=options)
# 启动后直接访问Google,无需重新登录
driver.get("https://www.google.com")
search_field = driver.find_element(By.NAME, "q")
search_field.send_keys("pizza")
search_field.send_keys(Keys.RETURN)

注意:启动前要关闭所有Chrome窗口,否则会触发权限冲突报错。

3. 使用undetected-chromedriver库

这个库专门针对反自动化检测优化,无需手动配置参数:

  1. 安装库:
pip install undetected-chromedriver
  1. 替换原有代码:
import undetected_chromedriver as uc
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time

# 初始化浏览器(自动绕过检测)
driver = uc.Chrome()

# 登录流程
driver.get("https://accounts.google.com/signin")
username = driver.find_element(By.NAME, "identifier")
username.send_keys("blabla@gmail.com")
username.send_keys(Keys.RETURN)
time.sleep(2)

password = driver.find_element(By.NAME, "password")
password.send_keys("asdf1234")
password.send_keys(Keys.RETURN)

# 搜索流程
driver.implicitly_wait(5)
driver.get("https://www.google.com")
search_field = driver.find_element(By.NAME, "q")
search_field.send_keys("pizza")
search_field.send_keys(Keys.RETURN)

内容的提问来源于stack exchange,提问作者Daniel Hernández

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 06:15:03