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



我正在使用Selenide框架和Java来编写一些自动化测试。 我的测试之一是文件下载。单击下载按钮后,Chrome会显示消息"此类文件可能会损害您的计算机。你是否要保留文件.xml?是否可以禁用此功能?我尝试了下面的代码,但它对我不起作用。

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的类

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);
}
}

相关内容

最新更新