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

使用Selenium 3.12+ChromeDriver 125时XML文件下载遭安全拦截问题

问题:Chrome拦截XML文件下载,多参数配置无效

我用的是Selenium 3.12版本,搭配适配Chrome 125的ChromeDriver,试过网上能找到的各种参数和配置,但运行时Chrome还是把XML文件标记为不安全,直接拦截下载。当前没有用无头模式,已配置的WebDriver参数如下:

HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.directory_upgrade", true);
chromePrefs.put("download.default_directory", savePAth);
chromePrefs.put("safebrowsing.enabled", false);
chromePrefs.put("download.prompt_for_download", false);
chromePrefs.put("plugins.always_open_pdf_externally", true);
chromePrefs.put("plugins.plugins_disabled", new String[]{"Adobe Flash Player", "Chrome PDF Viewer"});
chromePrefs.put("intl.accept_languages", "en");
chromePrefs.put("download_restrictions", 0);
chromePrefs.put("download.extensions_to_open", "xml");

ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("test-type");
options.addArguments("--disable-application-cache");
options.setAcceptInsecureCerts(true);
options.addArguments("--disable-web-security");
options.addArguments("--allow-running-insecure-content");
options.addArguments("--ignore-certificate-errors");
options.addArguments("--disable-popup-blocking");
options.addArguments("--lang=en");
options.addArguments("--no-sandbox");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--disable-password-manager");
options.addArguments("--remote-allow-origins=*");
options.addArguments("--disable-features=InsecureDownloadWarnings");
options.addArguments("--unsafely-treat-insecure-origin-as-secure={url}");
options.addArguments("--safebrowsing-disable-download-protection");

可尝试的解决方案

1. 补充安全浏览相关偏好设置

Chrome的安全浏览还有隐藏配置,在chromePrefs里加这两个参数:

chromePrefs.put("safebrowsing.disable_download_protection", true);
chromePrefs.put("safebrowsing.malware_scanning_enabled", false);

2. 修正XML打开配置

如果是要下载而非打开XML,把download.extensions_to_open设为空字符串:

chromePrefs.put("download.extensions_to_open", "");

3. 添加MIME类型映射

显式把XML的MIME类型设为允许,添加到chromePrefs:

chromePrefs.put("plugins.allowed_mime_types", "text/xml,application/xml");

4. 升级Selenium版本

Selenium 3.12是2018年的旧版本,和Chrome 125兼容性很差,很多新参数识别不了。建议直接升级到Selenium 4.x(至少4.10以上),新版对高版本Chrome支持更到位。

5. 模拟点击提示框(临时方案)

如果上面的方法都没用,可以试试模拟点击Chrome的下载确认提示框,不过这种方法依赖页面元素,稳定性一般:

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("download-confirm"))).click();

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 10:19:54