如何覆盖默认下载路径? (使用最新的Chrome驱动程序-2.35) 下面的代码将文件放在默认下载位置而不是指定的位置。
driver.get("page where download file exists");
driver.findElement(By.id("btnDownload")).click();
String downloadFilepath = "D:\\web driver\\";
Map<String, Object> preferences = new Hashtable<String, Object>();
preferences.put("profile.default_content_settings.popups", 0);
//preferences.put("download.prompt_for_download", "false");
preferences.put("download.default_directory", downloadFilepath);
System.out.println(downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", preferences);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
请帮帮我。
答案 0 :(得分:0)
我假设您正在使用最新的Selenium
版本。仅使用Capabilities
构建的旧方法已弃用。您应该使用ChromeOptions
代替。
试试这个,它应该在D
驱动器中下载文件。
String downloadFilepath = "D:\\web driver\\";
Map<String, Object> preferences = new Hashtable<String, Object>();
preferences.put("profile.default_content_settings.popups", 0);
preferences.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", preferences);
System.setProperty("webdriver.chrome.driver", "<your driver path>");
WebDriver driver = new ChromeDriver(options); //Launch the browser
driver.get("page where download file exists");
driver.findElement(By.id("btnDownload")).click();