ChromeDriver 121+强制弹出“另存为”对话框,如何绕过?(Selenium/Java)
Chrome 121+ 版本下 Selenium 文件下载自动化及隐身模式配置方案
一、解决“另存为”弹窗强制弹出问题
Chrome 121+ 调整了下载权限机制,旧版本的偏好配置已无法抑制弹窗,需补充以下配置项:
// 指定固定下载目录(替换为你的本地绝对路径) options.addPreference("download.default_directory", "D:\\AutoDownloads"); // 关闭下载保存位置询问 options.addPreference("download.prompt_for_download", false); // 允许自动提升下载目录权限 options.addPreference("download.directory_upgrade", true); // 拦截弹窗类请求 options.addPreference("profile.default_content_settings.popups", 0); // 关键配置:解除下载限制,允许无确认下载 options.addPreference("download.restrictions", 0); // 关闭下载管理器启动时的提示 options.addPreference("browser.download.manager.showWhenStarting", false);
二、启用隐身模式
直接添加标准启动参数即可,注意是双横线开头(单横线会导致参数无效):
options.addArguments("--incognito");
三、替代无效的 disable-features 参数
你之前使用的disable-features=DownloadBubble,DownloadBubbleV2在Chrome 121+ 中失效,可尝试补充新特性名称,或直接依赖第一部分的偏好配置来抑制弹窗:
options.addArguments("--disable-features=DownloadBubble,DownloadBubbleV2,DownloadNotification");
优先推荐用第一部分的偏好配置,适配新版本Chrome更稳定。
完整配置示例
ChromeOptions options = new ChromeOptions(); // 开启隐身模式 options.addArguments("--incognito"); // 配置自动下载(适配Chrome 121+) options.addPreference("download.default_directory", "D:\\AutoDownloads"); options.addPreference("download.prompt_for_download", false); options.addPreference("download.directory_upgrade", true); options.addPreference("profile.default_content_settings.popups", 0); options.addPreference("download.restrictions", 0); options.addPreference("browser.download.manager.showWhenStarting", false); // 备选:禁用下载气泡相关特性 options.addArguments("--disable-features=DownloadBubble,DownloadBubbleV2"); WebDriver driver = new ChromeDriver(options);
内容的提问来源于stack exchange,提问作者NILESH ACHLARE
相关产品推荐
相关产品推荐

