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

Selenium添加User-data-Dir后报错原因及配置问题排查

Selenium加载Chrome默认配置文件失败及DevToolsActivePort错误处理

问题背景

重置Windows系统并将Python从3.9升级到3.11后,原有Selenium加载Chrome默认配置文件的代码出现两种异常:

情况1:指定完整Default配置路径时

使用以下代码时,Chrome无法加载默认配置,仅打开无配置的浏览器:

from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service

chrome_userdata_dir = 'C:\\Users\\abdul\\AppData\\Local\\Google\\Chrome\\User Data\\Default'
option = Options()
option.add_argument(f"--user-data-dir={chrome_userdata_dir}")
option.add_argument('--profile-directory=Default')
driver = Chrome(service=Service(ChromeDriverManager().install()), options=option)

情况2:修改为User Data根路径时

将路径改为chrome_userdata_dir = 'C:\\Users\\abdul\\AppData\\Local\\Google\\Chrome\\User Data'后,Chrome能加载默认配置,但抛出WebDriverException,错误信息包含DevToolsActivePort文件不存在:

Opening in existing browser session.
Traceback (most recent call last):
  File "E:\LinuxData\Documents\TwitchVideoAutomator\main.py", line 135, in <module>
    automate_yt_pals(_headless)
  File "E:\LinuxData\Documents\TwitchVideoAutomator\subpal_subscribers\automate_yt_pals.py", line 56, in automate_yt_pals
    driver = Chrome(service=Service(path), options=option)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\LinuxData\Documents\TwitchVideoAutomator\venv\Lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 80, in __init__
    super().__init__(
  File "E:\LinuxData\Documents\TwitchVideoAutomator\venv\Lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 104, in __init__
    super().__init__(
  File "E:\LinuxData\Documents\TwitchVideoAutomator\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 286, in __init__
    self.start_session(capabilities, browser_profile)
  File "E:\LinuxData\Documents\TwitchVideoAutomator\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 378, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\LinuxData\Documents\TwitchVideoAutomator\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute
    self.error_handler.check_response(response)
  File "E:\LinuxData\Documents\TwitchVideoAutomator\venv\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, 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) [0x005B37D3]
        (No symbol) [0x00548B81]
        (No symbol) [0x0044B36D]
        (No symbol) [0x0046B7EF]
        (No symbol) [0x00466FC9]
        (No symbol) [0x004A1ED5]
        (No symbol) [0x004A1B2C]
        (No symbol) [0x0049B216]
        (No symbol) [0x00470D97]
        (No symbol) [0x0047253D]
        GetHandleVerifier [0x0082ABF2+2510930]
        GetHandleVerifier [0x00858EC1+2700065]
        GetHandleVerifier [0x0085C86C+2714828]
        GetHandleVerifier [0x00663480+645344]
        (No symbol) [0x00550FD2]
        (No symbol) [0x00556C68]
        (No symbol) [0x00556D4B]
        (No symbol) [0x00560D6B]
        BaseThreadInitThunk [0x76B27D69+25]
        RtlInitializeExceptionChain [0x77AFB74B+107]
        RtlClearBits [0x77AFB6CF+191]

当前环境

  • Python 3.11
  • selenium==4.8.2
  • webdriver-manager==3.8.5
  • Chrome配置文件路径(chrome://version查看):C:\Users\abdul\AppData\Local\Google\Chrome\User Data\Default
  • 补充:ChromeDriver与Chrome版本不匹配

解决方案

1. 修复ChromeDriver与Chrome版本不匹配问题

这是核心问题之一,可手动指定Chrome版本安装对应驱动:

from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.utils import ChromeType

# 替换为你的Chrome实际版本号,例如124.0.6367.60
driver_path = ChromeDriverManager(chrome_type=ChromeType.GOOGLE, version="124.0.6367.60").install()

也可先关闭所有Chrome进程,再重新运行代码让webdriver-manager自动匹配最新驱动。

2. 正确配置Chrome用户数据路径

--user-data-dir需指向User Data文件夹根目录,而非Default子文件夹,再通过--profile-directory指定配置文件名称。同时确保无其他Chrome进程运行:

from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
import psutil

# 关闭所有Chrome进程(避免会话冲突)
for proc in psutil.process_iter(['name']):
    if proc.info['name'] == 'chrome.exe':
        try:
            proc.kill()
        except:
            pass

# 配置路径
chrome_userdata_dir = 'C:\\Users\\abdul\\AppData\\Local\\Google\\Chrome\\User Data'
option = Options()
option.add_argument(f"--user-data-dir={chrome_userdata_dir}")
option.add_argument('--profile-directory=Default')
# 添加参数规避DevToolsActivePort问题
option.add_argument('--no-first-run')
option.add_argument('--no-service-autorun')
option.add_argument('--password-store=basic')

# 启动驱动
driver = Chrome(service=Service(ChromeDriverManager().install()), options=option)

3. 验证路径层级

通过chrome://version确认的路径中,User Data是用户数据根目录,Default是其中的默认配置文件夹,参数设置必须遵循这个层级,不能直接将--user-data-dir指向Default文件夹。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 05:47:57