为什么我不能使用Selenium Webdriver通过Firefox下载文件?

我试图使用配置文件设置在Firefox中下载文件,但它不起作用你能告诉我我做错了什么,我使用的代码发布在这一行下面

var profile = new FirefoxProfile { EnableNativeEvents = true }; profile.SetPreference("browser.download.folderList", 2); profile.SetPreference("browser.download.manager.showWhenStarting", false); profile.SetPreference("browser.download.dir", folderName); profile.SetPreference("browser.download.downloadDir", folderName); profile.SetPreference("browser.download.defaultFolder", folderName); profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "image/jpeg,application/vnd.oasis.opendocument.text,application/vnd.oasis.opendocument.spreadsheet," + "application/vnd.oasis.opendocument.presentation,application/vnd.oasis.opendocument.graphics," + "application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet," + "application/vnd.ms-powerpoint,application/vnd.openxmlformats-officedocument.presentationml.presentation," + "application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.mozilla.xul+xml," + "application/vnd.google-earth.kml+xml"); 

经过几天的尝试,并阅读了很多可能性,这个为我工作,所以我与你分享,我希望它有用:我只是这样设置webdriver firefox配置文件:

 firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream doc xls pdf txt"); 

这个解决方案让我避免显示firefox下载弹出窗口,我可以使用selenium webdriver自动下载XLS文件。

Selenium为每次运行创建一个新的firefox配置文件。 您需要为selenium创建一个firefox配置文件,并让您的selenium脚本使用它。 如果您在此配置文件上设置自动下载,那么它应该可以正常工作!

请参阅http://girliemangalo.wordpress.com/2009/02/05/creating-firefox-profile-for-your-selenium-rc-tests/

我只为java做过这个,但我想这个方法会类似。

编辑指定配置文件的java代码是:

 ProfilesIni profile = new ProfilesIni(); FirefoxProfile ffprofile = profile.getProfile("SELENIUM"); WebDriver driver = new FirefoxDriver(ffprofile); 

资源:

Selenium WebDriver默认使用什么配置文件?