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

Selenium无法打开指定Chrome配置文件问题求助

问题分析与解决办法

你代码里的核心错误是**user-data-dir的路径设置错误**——它应该指向Chrome的User Data根目录,而非直接指向具体的Profile文件夹。同时要确保运行代码前关闭所有Chrome窗口,否则配置文件会被锁定无法加载。

修正后的代码

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import time

chrome_options = Options()
# user-data-dir指向User Data根目录,不要包含Profile子文件夹
chrome_options.add_argument(r"user-data-dir=C:\Users\My Name\AppData\Local\Google\Chrome\User Data")
chrome_options.add_argument("--start-maximized")
# 用profile-directory指定具体的配置文件夹
chrome_options.add_argument("profile-directory=Profile 12")

driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.example.com")

额外排查要点

  • 确认Profile路径正确性:打开Chrome的Profile 12配置窗口,在地址栏输入chrome://version/,查看「个人资料路径」字段,确保代码里的user-data-dir是路径中User Data部分,profile-directory是最后一级文件夹名称(比如可能实际是Profile 1而非Profile 12,以实际路径为准)。
  • 关闭所有Chrome进程:运行代码前,打开任务管理器结束所有chrome.exe进程,否则Chrome会锁定配置文件,导致Selenium无法加载指定配置。
  • 路径转义问题:Windows路径可以用原始字符串前缀r""避免双反斜杠的转义麻烦,减少出错概率。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 14:34:50