如何在硒 Chrome 功能中设置默认下载目录?
2022-09-01 07:12:04
请找到以下代码,其中包含镶边功能。实际上,浏览器不会将文件下载到指定的路径。
private static DesiredCapabilities getChromeCapabilities() throws Exception {
String chromePath = BrowserUtil.class.getResource("/Browserdrivers/chromedriver.exe").getPath();
System.setProperty("webdriver.chrome.driver", chromePath);
String downloadFilepath = "C:\\TestDownloads";
ChromeOptions options = new ChromeOptions();
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("--test-type");
options.addArguments("start-maximized", "disable-popup-blocking");
DesiredCapabilities chromeCapabilities = DesiredCapabilities.chrome();
setProxy(chromeCapabilities);
chromeCapabilities.setPlatform(Platform.WINDOWS);
chromeCapabilities.setCapability("name", MDC.get("testname"));
chromeCapabilities.setCapability(ChromeOptions.CAPABILITY, options);
return chromeCapabilities;
}