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

Selenium调用Chrome本地用户配置文件失败求助

问题

使用Python结合Selenium实现浏览器自动化时,需要调用Chrome配置文件里的登录凭据,创建新配置文件后程序无法读取,相关代码如下:

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
import time, os

CHROME_USER_DIR = "C:/Users/LocalUserName/AppData/Local/Google/Chrome/User Data"
PROFILE_NAME = "Profile 1"

options = Options()
options.add_argument(f"--user-data-dir={CHROME_USER_DIR}")
options.add_argument(f"--profile-directory={PROFILE_NAME}")
driver = webdriver.Chrome(options=options)

运行后报错,关键错误信息为:

selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from chrome not reachable

完整错误日志:

DevTools remote debugging requires a non-default data directory. Specify this using --user-data-dir.
[12252:10400:0601/205536.845:ERROR:gpu\ipc\service\gpu_channel_manager.cc:946] Failed to create GLES3 context, fallback to GLES2.
[12252:10400:0601/205536.845:ERROR:gpu\ipc\service\gpu_channel_manager.cc:957] ContextResult::kFatalFailure: Failed to create shared context for virtualization.
[12252:10400:0601/205536.960:ERROR:gpu\ipc\service\gpu_channel_manager.cc:946] Failed to create GLES3 context, fallback to GLES2.
[12252:10400:0601/205536.960:ERROR:gpu\ipc\service\gpu_channel_manager.cc:957] ContextResult::kFatalFailure: Failed to create shared context for virtualization.
[12252:10400:0601/205536.960:ERROR:gpu\ipc\service\gpu_channel_manager.cc:946] Failed to create GLES3 context, fallback to GLES2.
[12252:10400:0601/205536.960:ERROR:gpu\ipc\service\gpu_channel_manager.cc:957] ContextResult::kFatalFailure: Failed to create shared context for virtualization.
[6180:10560:0601/205539.087:ERROR:chrome\browser\policy\cloud\fm_registration_token_uploader.cc:179] Client is missing for kUser scope
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1748800539.099043    2104 voice_transcription.cc:58] Registering VoiceTranscriptionCapability
Created TensorFlow Lite XNNPACK delegate for CPU.
[6180:5096:0601/205539.234:ERROR:google_apis\gcm\engine\registration_request.cc:291] Registration response error message: DEPRECATED_ENDPOINT
[6180:1708:0601/205539.382:ERROR:components\system_cpu\cpu_probe_win.cc:112] PdhAddEnglishCounter failed for '\Processor(_Total)\% Processor Time': Error (0x13D) while retrieving error. (0xC0000BB8)
[6180:1864:0601/205539.382:ERROR:components\system_cpu\cpu_probe_win.cc:112] PdhAddEnglishCounter failed for '\Processor(_Total)\% Processor Time': Error (0x13D) while retrieving error. (0xC0000BB8)
[6180:1708:0601/205551.758:ERROR:components\system_cpu\cpu_probe_win.cc:112] PdhAddEnglishCounter failed for '\Processor(_Total)\% Processor Time': Error (0x13D) while retrieving error. (0xC0000BB8)
[6180:5096:0601/205605.808:ERROR:google_apis\gcm\engine\registration_request.cc:291] Registration response error message: DEPRECATED_ENDPOINT
[6180:1708:0601/205606.768:ERROR:components\system_cpu\cpu_probe_win.cc:112] PdhAddEnglishCounter failed for '\Processor(_Total)\% Processor Time': Error (0x13D) while retrieving error. (0xC0000BB8)
[6180:1708:0601/205621.778:ERROR:components\system_cpu\cpu_probe_win.cc:112] PdhAddEnglishCounter failed for '\Processor(_Total)\% Processor Time': Error (0x13D) while retrieving error. (0xC0000BB8)
[6180:1708:0601/205636.778:ERROR:components\system_cpu\cpu_probe_win.cc:112] PdhAddEnglishCounter failed for '\Processor(_Total)\% Processor Time': Error (0x13D) while retrieving error. (0xC0000BB8)
Traceback (most recent call last):
  File "D:\Work\script_runner.py", line 38, in <module>
    driver = webdriver.Chrome(options=options)
  File "C:\Users\LocalUserName\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 47, in __init__
    super().__init__(
    ~~~~~~~~~~~~~~~~^
        browser_name=DesiredCapabilities.CHROME["browserName"],
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<3 lines>...
        keep_alive=keep_alive,
        ^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\Users\LocalUserName\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 69, in __init__
    super().__init__(command_executor=executor, options=options)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\LocalUserName\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 257, in __init__
    self.start_session(capabilities)
    ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "C:\Users\LocalUserName\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 356, in start_session
    response = self.execute(Command.NEW_SESSION, caps)["value"]
               ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\LocalUserName\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 447, in execute
    self.error_handler.check_response(response)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "C:\Users\LocalUserName\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
from chrome not reachable
Stacktrace:
        GetHandleVerifier [0x0x7ff7c0f26f65+78965]
        GetHandleVerifier [0x0x7ff7c0f26fc0+79056]
        (No symbol) [0x0x7ff7c0cb9c0c]
        (No symbol) [0x0x7ff7c0caaa41]
        (No symbol) [0x0x7ff7c0cfceb2]
        (No symbol) [0x0x7ff7c0cf7a78]
        (No symbol) [0x0x7ff7c0cf297d]
        (No symbol) [0x0x7ff7c0d465be]
        (No symbol) [0x0x7ff7c0d45d50]
        (No symbol) [0x0x7ff7c0d38443]
        (No symbol) [0x0x7ff7c0d01311]
        (No symbol) [0x0x7ff7c0d020a3]
        GetHandleVerifier [0x0x7ff7c11de26d+2926461]
        GetHandleVerifier [0x0x7ff7c11d8993+2903715]
        GetHandleVerifier [0x0x7ff7c11f6aed+3026941]
        GetHandleVerifier [0x0x7ff7c0f416fe+187406]
        GetHandleVerifier [0x0x7ff7c0f496ef+220159]
        GetHandleVerifier [0x0x7ff7c0f2faf4+114692]
        GetHandleVerifier [0x0x7ff7c0f2fca9+115129]
        GetHandleVerifier [0x0x7ff7c0f164d8+10728]
        BaseThreadInitThunk [0x0x7ff82e307374+20]
        RtlUserThreadStart [0x0x7ff82f9dcc91+33]

注:移除配置用户目录和配置文件的代码后程序可正常运行,但无法自动登录目标网站,页面显示异常。已确认ChromeDriver与Chrome版本一致,且运行程序时无其他应用使用该配置文件。


问题原因及解决办法

1. 默认用户目录权限/锁定问题

Chrome默认的User Data目录受系统权限保护,且Chrome会锁定该目录防止多实例冲突,Selenium直接调用时容易触发异常。

解决办法:

  • 复制目标配置文件夹(如Profile 1)到自定义目录,比如C:/ChromeProfiles/Profile1
  • 修改代码中的路径指向自定义目录:
CHROME_USER_DIR = "C:/ChromeProfiles"
PROFILE_NAME = "Profile1"

2. GPU渲染冲突

日志中出现GPU相关错误,说明Chrome的GPU渲染模块启动失败,导致会话无法创建。

解决办法:

  • 在Chrome选项中添加禁用GPU的参数:
options.add_argument("--disable-gpu")
options.add_argument("--disable-software-rasterizer")
options.add_argument("--headless=new")  # 可选,若不需要可视化界面

3. 残留进程占用配置文件

即使未手动打开Chrome,后台可能残留chrome.exe或chromedriver.exe进程,占用配置文件资源。

解决办法:

  • 运行程序前,打开任务管理器结束所有Chrome相关进程
  • 或在代码中添加进程清理逻辑(Windows系统):
import os
os.system("taskkill /f /im chrome.exe")
os.system("taskkill /f /im chromedriver.exe")

4. 路径格式错误

Windows路径中的空格或特殊字符可能导致Selenium解析失败,默认User Data目录名称包含空格,容易触发问题。

解决办法:

  • 确保自定义目录路径无空格,或用双反斜杠转义路径:
CHROME_USER_DIR = "C:\\ChromeProfiles"

验证步骤

  1. 手动打开Chrome,指定自定义配置目录,确认能正常登录目标网站:
chrome.exe --user-data-dir=C:/ChromeProfiles --profile-directory=Profile1
  1. 关闭手动打开的Chrome,再运行Selenium代码,确认会话正常创建。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 22:55:55