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

如何在SeleniumBase中指定特定Chrome用户配置文件?

问题:SeleniumBase无法加载指定的Chrome已有用户配置文件

原可正常运行的代码:

from seleniumbase import Driver

driver = Driver(uc=True)
driver.get("https://example.com")
driver.click("a")
p_text = driver.find_element("p").text
print(p_text)

尝试添加用户配置文件后的代码:

from seleniumbase import Driver

ud =  r"C:\Users\USER\AppData\Local\Google\Chrome\User Data\Profile 9"

driver = Driver(uc=True, user_data_dir=ud)
driver.get("https://example.com")
driver.click("a")
p_text = driver.find_element("p").text
print(p_text)

遇到的问题

  • 程序自动在指定路径后追加\Default,实际访问路径变为C:\Users\USER\AppData\Local\Google\Chrome\User Data\Profile 9\Default,而非目标的Profile 9路径
  • 自动创建了名为“person 1”的新配置文件,无法访问已存在的Profile 9

补充信息

正常启动该配置文件的命令行参数:

"C:\Program Files\Google\Chrome\Application\chrome.exe" --window-size=1280,840 --disable-dev-shm-usage --disable-application-cache --disable-browser-side-navigation --disable-save-password-bubble --disable-single-click-autofill --allow-file-access-from-files --disable-prompt-on-repost --dns-prefetch-disable --disable-translate --disable-renderer-backgrounding --disable-backgrounding-occluded-windows --disable-features=OptimizationHintsFetching,OptimizationTargetPrediction --disable-popup-blocking --homepage=chrome://new-tab-page/ --remote-debugging-host=127.0.0.1 --remote-debugging-port=53654 --user-data-dir="C:\Users\fun64\AppData\Local\Google\Chrome\User Data\Profile 9" --lang=en-US --no-default-browser-check --no-first-run --no-service-autorun --password-store=basic --log-level=0 --flag-switches-begin --flag-switches-end --origin-trial-disabled-features=WebGPU
  • Chrome可执行文件路径:C:\Program Files\Google\Chrome\Application\chrome.exe
  • 目标配置文件实际路径:C:\Users\fun64\AppData\Local\Google\Chrome\User Data\Profile 9\Default

解决方案

SeleniumBase的user_data_dir参数对应Chrome的--user-data-dir,这个参数需要传入的是Chrome用户数据的根目录(即包含所有Profile文件夹的User Data目录),而非单个Profile的路径。要指定具体配置文件,需额外添加--profile-directory参数。

修改后的代码:

from seleniumbase import Driver

# 配置Chrome用户数据根目录(Profile 9的上级目录)
user_data_root = r"C:\Users\USER\AppData\Local\Google\Chrome\User Data"
# 指定要加载的配置文件夹名称
target_profile = "Profile 9"

driver = Driver(
    uc=True,
    user_data_dir=user_data_root,
    options=[f"--profile-directory={target_profile}"]
)
driver.get("https://example.com")
driver.click("a")
p_text = driver.find_element("p").text
print(p_text)

注意事项

  • 确保Chrome后台没有运行该配置文件的进程,否则会因Chrome的单实例锁导致加载失败
  • user_data_root必须是包含所有Profile的根目录,不能直接写单个Profile的路径
  • 通过options传入--profile-directory参数,明确指定要加载的配置文件夹,避免程序自动追加\Default路径

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 16:57:41