一尘不染

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

selenium

我正在使用seleniumjava工作,需要下载pdf文件,但是在我的情况下似乎没有任何效果。是由于设置了新的firefox驱动程序实例ieSystem.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);

阅读 397

收藏
2020-06-26

共1个答案

一尘不染

以下代码块使用直通绑定配置Firefox Profileto DownloadSavePDF文件:Selenium``Java

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);
2020-06-26