如何使用FirefoxProfile通过Firefox浏览器自动下载?

我正在使用selenium java,我需要下载pdf文件,我在这里提到thisthisthis答案,但似乎没有什么工作在我的情况 . 是因为设置了一个新的firefox驱动程序实例,即 System.setProperty("webdriver.firefox.bin", "D:\\FFF\\firefox.exe"); ?我被困在这里 . 但是当我手动点击MIME对话框上的保存文件时,它正确地保存到我的自定义位置,我的下载链接代码也驻留在另一个java类和下面的代码中,但是我使用相同的驱动程序在这个类中声明,下面是我的代码,

FirefoxProfile profile = new FirefoxProfile();
//Set Location to store files after downloading.
profile.setPreference("browser.download.folderList", 2);
profile.setPreference( "browser.download.manager.showWhenStarting", false );
profile.setPreference("browser.download.dir", "D:\\WebDriverDownloads");
profile.setPreference("pdfjs.disabled", true);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf"); 
System.setProperty("webdriver.firefox.bin", "D:\\FFF\\firefox.exe");
driver = new FirefoxDriver(profile);

回答(1)

3 years ago

以下代码块使用 SeleniumJava 绑定配置 Firefox ProfileDownloadSave PDF文件:

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.dir", "C:\\Utility\\Downloads");
profile.setPreference("browser.download.folderList",2);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/plain,application/octet-stream,application/pdf,application/x-pdf,application/vnd.pdf");
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("browser.helperApps.neverAsk.openFile","text/plain,application/octet-stream,application/pdf,application/x-pdf,application/vnd.pdf");
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.useWindow", false);
profile.setPreference("browser.download.manager.focusWhenStarting", false);
profile.setPreference("browser.helperApps.neverAsk.openFile", "");
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
profile.setPreference("browser.download.manager.showAlertOnComplete", false);
profile.setPreference("browser.download.manager.closeWhenDone", true);
profile.setPreference("pdfjs.disabled", true);
System.setProperty("webdriver.firefox.bin", "D:\\FFF\\firefox.exe");
WebDriver driver = new FirefoxDriver(profile);