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

Chrome出现DevToolsActivePort文件不存在错误,添加调试端口后无法多实例运行

解决Chrome多实例启动冲突与DevToolsActivePort问题

问题核心

之前能正常多开Chrome自动化实例,出现「DevToolsActivePort file doesn't exist」错误后,仅能通过添加--remote-debugging-port参数恢复运行,但添加后无法并发/顺序启动新实例:

  • 启动第二个实例时提示chrome not reachable,ChromeDriver判定浏览器崩溃
  • 已有实例运行时,新实例会显示Opening in existing browser session,无法独立启动

问题原因

仅指定随机调试端口但未配置独立的用户数据目录,Chrome会自动复用已有会话的用户数据,导致新实例无法创建独立进程,进而触发崩溃报错或会话复用。

解决方案

在Chrome启动参数中同时添加--user-data-dir,为每个实例分配唯一的临时用户数据目录,彻底隔离不同实例的运行环境。

修改后的代码

# selenium模块
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service as ChromiumService
# webdriver manager模块
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.utils import ChromeType
# 工具模块
from random import randrange
import tempfile

def simple_chrome(page=""):
    options = Options()
    # 生成随机调试端口
    debugging_port = str(randrange(9222, 9999))
    options.add_argument(f'--remote-debugging-port={debugging_port}')
    # 创建唯一临时用户数据目录
    temp_dir = tempfile.mkdtemp(prefix="chrome_auto_")
    options.add_argument(f'--user-data-dir={temp_dir}')
    # 可选优化:禁用GPU加速,减少环境兼容性问题
    options.add_argument('--disable-gpu')

    service = ChromiumService(ChromeDriverManager().install())
    browser = webdriver.Chrome(service=service, options=options)
    browser.get(page)
    return browser

# 测试多实例启动
browser = simple_chrome("https://docs.python.org/3/library/shutil.html")
browser2 = simple_chrome("https://apnews.com")

关键说明

  • --user-data-dir:每个实例使用独立的用户数据目录,从根源避免Chrome复用会话或进程冲突
  • 用tempfile.mkdtemp自动生成唯一临时目录,无需手动维护路径唯一性
  • --disable-gpu为可选参数,能规避部分Windows环境下的渲染异常问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 04:57:33