How to auto-download through Firefox browser using FirefoxProfile?

一曲冷凌霜 提交于 2019-11-26 04:28:19

问题


I\'m working with selenium java where I need to download pdf files, I referred this, this and also this answers here, but seems like nothing is working in my situation. Is it due to setting a new firefox driver instance i.e.System.setProperty(\"webdriver.firefox.bin\", \"D:\\\\FFF\\\\firefox.exe\"); ? I\'m stuck here.However when I manually click on save file on the MIME dialog it saves correctly to my custom location, also my download link code resides in another java class and below code in another class , but I use the same driver as declared in this class, below is my code,

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:


The following code block configures a Firefox Profile to Download and Save PDF files using Selenium through Java bindings:

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);


来源:https://stackoverflow.com/questions/45589571/how-to-auto-download-through-firefox-browser-using-firefoxprofile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!