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

如何使用Python Selenium WebDriver加载Chrome自定义配置文件?报错求助

问题:Selenium启动Chrome自定义配置文件失败,报错DevToolsActivePort不存在

我尝试使用Python Selenium WebDriver启动Chrome自定义配置文件,但运行时出现错误。已尝试添加--no-sandbox、--disable-dev-shm-usage、--remote-debugging-port=xxxx参数,但均无效。

我的代码:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time

options = webdriver.ChromeOptions()
options.add_argument("--profile-directory=Profile 6")
options.add_argument("--user-data-dir=C:/Users/SIM/AppData/Local/Google/Chrome/User Data")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
time.sleep(3)

运行输出:

D:\me\web\fbot\src>python script_.py
Opening in existing browser session.
[7488:16428:1223/013230.522:ERROR:broker_win.cc(56)] Error reading broker pipe: The pipe has been ended. (0x6D)
Traceback (most recent call last):
  File "D:\me\web\fbot\src\script_.py", line 10, in <module>
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
  File "C:\Python310\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 81, in __init__      
    super().__init__(
  File "C:\Python310\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 106, in __init__   
    super().__init__(
  File "C:\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 288, in __init__     
    self.start_session(capabilities, browser_profile)
  File "C:\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 381, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 444, in execute
    self.error_handler.check_response(response)
  File "C:\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 249, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited normally.
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location C:\Program Files\Google\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Stacktrace:
Backtrace:
        (No symbol) [0x0103F243]
        (No symbol) [0x00FC7FD1]
        (No symbol) [0x00EBD04D]
        (No symbol) [0x00EDC24E]
        (No symbol) [0x00ED82E9]
        (No symbol) [0x00F0F056]
        (No symbol) [0x00F0EB2A]
        (No symbol) [0x00F08386]
        (No symbol) [0x00EE163C]
        (No symbol) [0x00EE269D]
        GetHandleVerifier [0x012D9A22+2655074]
        GetHandleVerifier [0x012CCA24+2601828]
        GetHandleVerifier [0x010E8C0A+619850]
        GetHandleVerifier [0x010E7830+614768]
        (No symbol) [0x00FD05FC]
        (No symbol) [0x00FD5968]
        (No symbol) [0x00FD5A55]
        (No symbol) [0x00FE051B]
        BaseThreadInitThunk [0x7762FA29+25]
        RtlGetAppContainerNamedObjectPath [0x77CD7A7E+286]
        RtlGetAppContainerNamedObjectPath [0x77CD7A4E+238]

解决方法:

  • 关闭所有Chrome进程
    错误提示里的Opening in existing browser session说明当前有Chrome实例在占用目标用户数据目录,Selenium无法获取控制权。打开任务管理器,结束所有chrome.exe和chromedriver.exe进程后再运行脚本。

  • 修正路径格式(避免转义问题)
    将用户数据目录路径改为原始字符串格式,防止转义字符干扰:

    options.add_argument(r"--user-data-dir=C:\Users\SIM\AppData\Local\Google\Chrome\User Data")
    
  • 添加初始化屏蔽参数
    加入--no-first-run和--no-default-browser-check,避免Chrome启动时的初始化弹窗打断会话创建:

    options.add_argument("--no-first-run")
    options.add_argument("--no-default-browser-check")
    
  • 验证配置文件路径正确性
    确认Profile 6文件夹确实存在于User Data目录下,部分场景下Chrome配置文件夹名称可能为Profile 1或其他,需核对实际路径。

  • 完整修改后的代码示例:

    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from webdriver_manager.chrome import ChromeDriverManager
    import time
    
    options = webdriver.ChromeOptions()
    # 使用原始字符串避免转义
    options.add_argument(r"--user-data-dir=C:\Users\SIM\AppData\Local\Google\Chrome\User Data")
    options.add_argument("--profile-directory=Profile 6")
    # 屏蔽初始化弹窗
    options.add_argument("--no-first-run")
    options.add_argument("--no-default-browser-check")
    # 启用远程调试端口(可选)
    options.add_argument("--remote-debugging-port=9222")
    
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
    time.sleep(3)
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 04:05:22