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

如何通过Bat脚本或Selenium为Chrome特定配置文件设置下载目录

为Chrome特定配置文件设置专属下载目录(非手动方式)

我需要为Google Chrome的特定配置文件设置专属下载目录,要求不通过手动操作,而是用命令行或其他自动化方式实现。

我当前使用的ChromeOptions配置如下:

options.add_argument('--allow-profiles-outside-user-dir')
options.add_argument('--enable-profile-shortcut-manager')
options.add_argument(rf'user-data-dir=C:\User')
options.add_argument('--profile-directory=Profile 1')
options.add_argument('ignore-certificate-errors')

我尝试过以下操作,但均未生效:

  • 自行编写脚本,未成功;
  • 查找Chrome偏好设置文件,但没找到下载目录相关配置项,可能路径不对;
  • 尝试在ChromeOptions中添加options.add_argument(f"download.default_directory={download_path}"),无效;
  • 编写了Bat脚本,示例如下:
@echo off
"C:\Program Files\Google\Chrome\Application\chrome.exe" --user-data-dir="C:\Path\To\Your\Custom\Profile" --download.default_directory="C:\Path\To\Your\Download\Directory"

有效解决方法

方法1:直接修改Preferences配置文件(推荐)

Chrome的下载目录配置存放在对应配置文件的Preferences文件中,路径为:{你的user-data-dir路径}\{profile-directory}\Preferences(对应你的路径就是C:\User\Profile 1\Preferences)。

操作步骤:

  1. 关闭所有Chrome进程(包括后台运行的);
  2. 用文本编辑器打开Preferences文件;
  3. 找到"download"节点,修改或添加以下字段:
    "download": {
      "default_directory": "C:\\Your\\Custom\\Download\\Path",
      "directory_upgrade": true
    }
    
    注意:路径里的反斜杠要写成双反斜杠,directory_upgrade设为true是防止Chrome自动覆盖配置;
  4. 保存文件,重启Chrome即可生效。

方法2:修正命令行/ChromeOptions参数格式

你之前的参数传递方式有误,正确的写法如下:

Bat脚本启动
@echo off
"C:\Program Files\Google\Chrome\Application\chrome.exe" --user-data-dir="C:\User" --profile-directory="Profile 1" --args --download.default_directory="C:\Your\Download\Path" --download.directory_upgrade=true

关键是要通过--args传递Chrome的内部配置项,同时加上--download.directory_upgrade=true确保配置不被重置。

ChromeOptions(自动化场景)
download_path = r"C:\Your\Download\Path"
options.add_argument('--allow-profiles-outside-user-dir')
options.add_argument('--enable-profile-shortcut-manager')
options.add_argument(rf'user-data-dir=C:\User')
options.add_argument('--profile-directory=Profile 1')
options.add_argument('ignore-certificate-errors')
# 新增正确的下载目录参数
options.add_argument(f'--download.default_directory={download_path}')
options.add_argument('--download.directory_upgrade=true')
options.add_argument('--safebrowsing-disable-download-protection')

注意参数前要加--,之前的写法缺少短横线,导致Chrome无法识别该配置项。

注意事项

  • 修改Preferences文件时,必须完全关闭Chrome,否则修改会被Chrome的后台进程覆盖;
  • 命令行启动时,--user-data-dir和--profile-directory必须和目标配置文件的路径完全一致;
  • 如果是用Selenium等自动化工具,要确保ChromeDriver版本与Chrome版本严格匹配,否则参数可能不生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 05:32:47