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

Linux环境下如何用Selenium实现Chrome无弹窗自动下载文件

解决Selenium Chrome自动下载JSON文件不弹窗的问题

你的配置存在几个关键错误,导致无法屏蔽下载确认弹窗,修正后即可实现自动下载:

错误配置分析

  • profile.default_content_settings.popups = 1:该值设为1是允许弹窗,需改为0禁止弹窗
  • download.extensions_to_open = "application/json":此参数用于指定下载后自动打开的文件类型,JSON无需自动打开,应删除或设为空字符串
  • profile.default_content_setting_values.automatic_downloads = 0:设为0是禁止自动下载,需改为1允许自动下载

修正后的完整配置

ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("-js-flags=--expose-gc");
options.addArguments("--remote-allow-origins=*");
options.addArguments("--safebrowsing-disable-download-protection");
options.addArguments("--safebrowsing-disable-extension-blacklist");
// 新增:禁用PDF预览逻辑,避免影响JSON下载行为
options.addArguments("--disable-pdf-viewer");
// 新增:彻底禁用下载气泡弹窗
options.addArguments("--disable-features=DownloadBubble,DownloadBubbleV2");

Map<String, Object> pref = new HashMap<String, Object>();
pref.put("download.default_directory", getDownloadDir());
pref.put("download.directory_upgrade", true);
// 删除自动打开JSON的配置(或注释掉)
// pref.put("download.extensions_to_open", "application/json");
pref.put("profile.default_content_settings.popups", 0); // 禁止弹窗
pref.put("download.prompt_for_download", false); // 关闭下载提示弹窗
pref.put("profile.default_content_setting_values.automatic_downloads", 1); // 允许自动下载
pref.put("safebrowsing.enabled", true);
// 新增:针对所有域名允许自动下载JSON
pref.put("content_settings.exceptions.automatic_downloads.*.setting", 1);
pref.put("download.default_content_setting_values.automatic_downloads", 1);

options.setExperimentalOption("prefs", pref);

额外注意事项

  • 确保getDownloadDir()返回绝对路径,且目标目录存在、具备读写权限
  • 检查Chrome版本与Selenium、ChromeDriver版本是否兼容(建议使用对应大版本的驱动)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 05:42:50