这是我写的代码:
public static void main(String args[]){
FirefoxProfile ffProfile = new FirefoxProfile();
FirefoxBinary ffBinary = new FirefoxBinary();
ffBinary.setTimeout(TimeUnit.SECONDS.toMillis(60));
ffProfile.setPreference("browser.download.dir","C:\\ComonFiles");
ffProfile.setPreference("browser.download.folderList", 2);
ffProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;"
+ "application/pdf;"
+ "application/xml;"
+ "text/xml;"
+ "application/zip;"
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document;" + "text/plain;"
+ "text/csv");
System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe");
WebDriver driver = new FirefoxDriver(ffBinary, ffProfile);
//Some other operations
downloadFile1();
downloadFile2();
}
public static void downloadFile1()
{
//Click on download button
}
public static void downloadFile2()
{
ffProfile.setPreference("browser.download.dir","C:\\WorkingFiles");
//Click on download button
}
我想将 File2 下载到名为C:\\WorkingFiles
的单独文件夹中,但即使我在downloadFile2()
中再次调用设置首选项,它也不会覆盖下载设置。它总是将所有文件下载到C:\\ComonFiles
。它仍然使用主要功能的首选项进行下载。