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

Python使用undetected-chromedriver报错无法连接127.0.0.1端口Chrome如何解决

问题根源

你遇到的报错核心原因是系统内置的chromedriver版本过旧(v2.41为2018年发布的版本),与当前安装的undetected-chromedriver版本不兼容,且与Chrome浏览器版本不匹配。原生Selenium对旧版本驱动的兼容性阈值更高,所以可以正常运行,而undetected-chromedriver需要驱动与浏览器大版本完全一致才能正常工作。


解决步骤

  • 第一步:清理旧版本的Chrome浏览器与chromedriver
    执行命令卸载系统内置的旧版本:
apt purge -y chromium-browser chromium-chromedriver
rm -rf /usr/bin/chromedriver
  • 第二步:安装最新稳定版Chrome浏览器
    以Debian/Ubuntu系Linux为例,从Chrome官方下载最新稳定版的amd64安装包后,执行以下命令安装:
apt install -y ./google-chrome-stable_current_amd64.deb

安装完成后执行google-chrome --version确认版本正常。

  • 第三步:修改测试代码,不要手动指定chromedriver路径
    undetected-chromedriver会自动检测当前Chrome版本,下载对应匹配的驱动,无需手动指定路径。因为你是在root用户下运行,必须保留--no-sandbox参数,参考可用代码如下:
import undetected_chromedriver as uc

options = uc.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('--headless=new')
options.add_argument('--disable-dev-shm-usage')

driver = uc.Chrome(options=options)
with driver:
    driver.get('https://google.com')
    print(driver.title)

补充说明

  • 如果不需要图形界面,不需要额外安装Xvfb,直接使用Chrome原生的headless模式即可
  • 不要使用已经废弃的v2导入路径,最新版undetected-chromedriver直接导入undetected_chromedriver即可
  • 如果运行时提示依赖缺失,可先执行apt install -y libxss1 libappindicator1 libindicator7补全Chrome运行依赖
  • 可以提前升级undetected-chromedriver到最新版本:pip install -U undetected-chromedriver

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 19:24:02