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

Selenium 4.32.0加载Chrome默认配置文件失败,无法打开URL

问题描述

使用Selenium 4.32.0调用Chrome 136.0.7103.114正式版64位时,遇到以下问题:

  • 指定默认用户配置文件实现持久登录时,抛出SessionNotCreatedException,提示用户数据目录已被占用;即使关闭Chrome后执行脚本仍报错。
  • 注释配置文件相关参数后脚本可正常运行,但无法使用已登录的Chrome配置。

已尝试更换配置文件、升级ChromeDriver,问题未解决。

原代码

import time
import datetime
from datetime import datetime, timedelta, date
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException, TimeoutException, StaleElementReferenceException

options = Options()
options.add_argument(r"--user-data-dir=C:\Users\[username]\AppData\Local\Google\Chrome\User Data")  # ← 替换你的用户名
options.add_argument(r"--profile-directory=Default")

driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver, 20)

报错信息

DevTools remote debugging requires a non-default data directory. Specify this using --user-data-dir.
[9884:2348:0515/080007.592:ERROR:chrome\browser\ui\webui\ntp\new_tab_ui.cc:55] Requested load of chrome://newtab/ for incorrect profile type.
Created TensorFlow Lite XNNPACK delegate for CPU.
Attempting to use a delegate that only supports static-sized tensors with a graph that has dynamic-sized tensors (tensor#-1 is a dynamic-sized tensor).
[9884:22912:0515/080009.978:ERROR:google_apis\gcm\engine\registration_request.cc:291] Registration response error message: DEPRECATED_ENDPOINT
Traceback (most recent call last):
  File "c:\Users\username\OneDrive\Documents\Projects\peakz_padel_bot\peakz_padel_bot.py", line 21, in <module>
    driver = webdriver.Chrome(options=options)
  File "C:\Users\username\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 45, in __init__
    super().__init__(
    ~~~~~~~~~~~~~~~~^
        browser_name=DesiredCapabilities.CHROME["browserName"],
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<3 lines>...
        keep_alive=keep_alive,
        ^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\Users\username\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 67, in __init__
    super().__init__(command_executor=executor, options=options)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\username\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 260, in __init__
    self.start_session(capabilities)
    ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "C:\Users\username\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 357, in start_session
    response = self.execute(Command.NEW_SESSION, caps)["value"]
               ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\username\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 448, in execute
    self.error_handler.check_response(response)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "C:\Users\username\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line
 232, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: probably user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir
Stacktrace:
        GetHandleVerifier [0x00007FF6F83ACF65+75717]
        GetHandleVerifier [0x00007FF6F83ACFC0+75808]
        (No symbol) [0x00007FF6F8178F9A]
        (No symbol) [0x00007FF6F81B5B02]
        (No symbol) [0x00007FF6F81B1ADD]
        (No symbol) [0x00007FF6F82052D8]
        (No symbol) [0x00007FF6F82048B0]
        (No symbol) [0x00007FF6F81F7153]
        (No symbol) [0x00007FF6F81C0421]
        (No symbol) [0x00007FF6F81C11B3]
        GetHandleVerifier [0x00007FF6F86AD74D+3223469]
        GetHandleVerifier [0x00007FF6F86A7CF2+3200338]
        GetHandleVerifier [0x00007FF6F86C5B23+3322755]
        GetHandleVerifier [0x00007FF6F83C6A3A+180890]
        GetHandleVerifier [0x00007FF6F83CE13F+211359]
        GetHandleVerifier [0x00007FF6F83B52B4+109332]
        GetHandleVerifier [0x00007FF6F83B5462+109762]
        GetHandleVerifier [0x00007FF6F839BA79+4825]
        BaseThreadInitThunk [0x00007FF96B80E8D7+23]
        RtlUserThreadStart [0x00007FF96CA9C5DC+44]
解决方案

1. 彻底清除Chrome残留进程

  • 打开任务管理器,找到所有chrome.exe、chromedriver.exe进程,全部结束后再运行脚本。
  • 也可以用命令行强制终止:
taskkill /F /IM chrome.exe /IM chromedriver.exe

2. 使用独立的用户配置目录

不要直接使用Chrome默认的User Data目录(容易被手动打开的Chrome占用),复制一份到其他路径:

  1. 关闭所有Chrome窗口,复制C:\Users\[username]\AppData\Local\Google\Chrome\User Data到C:\Users\[username]\AppData\Local\Google\Chrome\Selenium_Profile
  2. 修改代码中的配置参数:
options.add_argument(r"--user-data-dir=C:\Users\[username]\AppData\Local\Google\Chrome\Selenium_Profile")
options.add_argument(r"--profile-directory=Default")

3. 添加额外启动参数避免冲突

在代码中添加以下参数,减少启动时的资源冲突:

options.add_argument("--no-first-run")  # 跳过首次运行设置
options.add_argument("--no-default-browser-check")  # 跳过默认浏览器检查
options.add_argument("--disable-dev-shm-usage")  # 禁用共享内存,避免资源占用问题
options.add_argument("--remote-debugging-port=9222")  # 指定固定调试端口,避免端口冲突

4. 确认版本完全匹配

  • 确保ChromeDriver版本与Chrome版本完全一致(均为136.0.7103.114),下载对应版本的ChromeDriver后,可放在Python安装目录或系统PATH中,也可以在代码中指定路径:
driver = webdriver.Chrome(executable_path=r"C:\path\to\chromedriver.exe", options=options)

5. 避免多实例冲突

确保运行脚本时,没有其他脚本或手动Chrome窗口使用同一个配置目录。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 03:34:56