如何在 chromedriver 中禁用 "This type of file can harm your computer. Do you want to keep file.xml anyway?"?

How disable "This type of file can harm your computer. Do you want to keep file.xml anyway?" in chromedriver?

我正在使用 Selenide 框架和 Java 编写一些自动化测试。 我的测试之一是文件下载。单击下载按钮后 Chrome 给出消息 "This type of file can harm your computer. Do you want to keep file.xml anyway?"。是否可以禁用此功能?我试过下面的代码,但它对我不起作用。

System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("safebrowsing.enabled", "true");
options.setExperimentalOption("prefs", prefs);
ChromeDriver chromeDriver = new ChromeDriver(options);
Configuration.browser = "chrome";

我找到了解决方案。 我只是创建了实现 WebDriverProvider

的 Class
public class CustomWebDriverProviderChrome implements WebDriverProvider {
    public WebDriver createDriver(DesiredCapabilities capabilities) {

        HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
        chromePrefs.put("profile.default_content_settings.popups", 0);
        chromePrefs.put("download.prompt_for_download", "true");
        chromePrefs.put("safebrowsing.enabled", "true");
        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("prefs", chromePrefs);
        DesiredCapabilities cap = DesiredCapabilities.chrome();
        cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        cap.setCapability(ChromeOptions.CAPABILITY, options);

        return new ChromeDriver(cap);
    }
}