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

在Google Colab中使用Undetectable Chrome Driver失败,求替代方案

在Google Colab中实现Undetectable Chrome Driver登录Gmail的替代方案

问题回顾

你在Google Colab中尝试运行Selenium代码登录Gmail,使用google-colab-selenium[undetected]和Seleniumbase均遇到以下错误:

SessionNotCreatedException: Message: session not created: cannot connect to chrome at 127.0.0.1:9222 from chrome not reachable

已执行的安装命令:

!pip install selenium
!apt-get update
!apt-get install -y chromium-browser
!apt install chromium-chromedriver
%pip install google-colab-selenium[undetected]

初始化驱动的代码:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import google_colab_selenium as gs
from seleniumbase import Driver

driver = gs.UndetectedChrome()
driver = Driver(uc=True, headless=True)

可行替代方案

1. 手动配置原版Undetected Chromedriver

绕过封装库,直接适配Colab的Chromium环境:

安装依赖

!pip install selenium undetected-chromedriver
!apt-get update
!apt-get install -y chromium-browser chromium-chromedriver
!ln -s /usr/lib/chromium-browser/chromedriver /usr/bin/chromedriver

初始化代码

import undetected_chromedriver as uc
from selenium.webdriver.chrome.options import Options

# 配置Chrome防检测选项
chrome_options = Options()
chrome_options.add_argument("--headless=new")  # 新版无头模式,降低检测概率
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
chrome_options.add_argument("--start-maximized")

# 初始化驱动
driver = uc.Chrome(options=chrome_options, driver_executable_path='/usr/bin/chromedriver')

# 访问Gmail
driver.get("https://mail.google.com/")

2. 使用Playwright替代Selenium

Playwright自带原生反检测机制,对Google服务兼容性更好,无需额外配置:

安装依赖

!pip install playwright
!playwright install chromium

初始化代码

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(headless=True, args=["--no-sandbox", "--disable-dev-shm-usage"])
    context = browser.new_context()
    page = context.new_page()
    
    # 跳转至Gmail
    page.goto("https://mail.google.com/")
    
    # 示例登录步骤(需替换为实际元素选择器)
    # page.fill('input[type="email"]', "你的邮箱地址")
    # page.click('button:has-text("下一步")')
    # page.fill('input[type="password"]', "你的密码")
    # page.click('button:has-text("下一步")')
    
    # 操作完成后关闭资源
    # browser.close()

3. 调整Seleniumbase启动参数

若坚持使用Seleniumbase,补充适配Colab的环境参数:

初始化代码

from seleniumbase import Driver

driver = Driver(
    uc=True,
    headless=True,
    no_sandbox=True,
    disable_dev_shm_usage=True,
    browser="chrome",
    driver_path="/usr/bin/chromedriver"
)

driver.get("https://mail.google.com/")

4. 排查Colab运行时环境

  • 确认当前运行时为Python 3环境,未处于资源受限状态
  • 尝试重启Colab运行时(菜单栏「Runtime」→「Restart runtime」)后重新安装依赖

内容的提问来源于stack exchange,提问作者stanabk

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 02:13:17