我正在使用Selenium web驱动程序并使用chrome with with我想设置下载位置但失败了

时间:2016-09-29 19:51:30

标签: java selenium webdriver

String desktopPath =System.getProperty("user.home") + "\\"+"Desktop";
String s = desktopPath.replace("\\","\\\\") ;

System.out.print(s);

System.setProperty("webdriver.chrome.driver","C:\\Users\\WaqaeAbbas\\Downloads\\Compressed\\chromedriver_win32\\chromedriver.exe");
String downloadFilepath = s;
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", "C:\\Users\\WaqaeAbbas\\Desktop");

ChromeOptions options = new ChromeOptions();
HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
options.setExperimentalOption("prefs", chromePrefs);

options.addArguments("--test-type");

DesiredCapabilities cap = DesiredCapabilities.chrome();

cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);

cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

cap.setCapability(ChromeOptions.CAPABILITY, options);   

WebDriver driver = new ChromeDriver(cap);

1 个答案:

答案 0 :(得分:0)

如果我们提供&#34; \\&#34;它就无法正常工作。因此,当我们通过下载路径时,它正在工作: String desktopPath =&#34; C:/ Users / WaqaeAbbas / Desktop&#34 ;;

以下是更新后的代码:

String desktopPath ="C:/Users/WaqaeAbbas/Desktop";
System.out.print(desktopPath);
System.setProperty("webdriver.chrome.driver","C:\\Users\\WaqaeAbbas\\Downloads\\Compressed\\chromedriver_win32\\chromedriver.exe");
String downloadFilepath = desktopPath;
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory",downloadFilepath );

ChromeOptions options = new ChromeOptions();
HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("--test-type");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);  
WebDriver driver = new ChromeDriver(cap);