如何使用FirefoxProfile通过Firefox浏览器自动下载? [英] How to auto-download through Firefox browser using FirefoxProfile?

查看:192
本文介绍了如何使用FirefoxProfile通过Firefox浏览器自动下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium Java,需要下载pdf文件,我提到了以及在这里回答,但在我的情况下似乎没有任何效果. 是由于设置了新的firefox驱动程序实例即System.setProperty("webdriver.firefox.bin", "D:\\FFF\\firefox.exe");吗?我被困在这里,但是当我在MIME对话框上手动单击保存文件时,它可以正确保存到我的自定义位置,我的下载链接代码也位于另一个Java类中,并且位于另一个类的下面,但是我使用的驱动程序与在此类中声明, 下面是我的代码,

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

推荐答案

以下代码块使用SeleniumJava绑定,将Firefox Profile分别配置为DownloadSave PDF文件:

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

这篇关于如何使用FirefoxProfile通过Firefox浏览器自动下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆