设置Firefox配置文件以使用Selenium和Java自动下载文件 [英] Set Firefox profile to download files automatically using Selenium and Java

查看:663
本文介绍了设置Firefox配置文件以使用Selenium和Java自动下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Selenium webdriver和Java验证文件下载。要下载的文件是pdf格式。当webdriver点击AUT中的下载链接时,firefox会打开以下下载确认窗口

I want to verify file download using Selenium webdriver and Java. The file to download is of pdf format. When webdriver clicks on 'Download' link in the AUT, firefox open up the following download confirmation window

我希望firefox自动下载文件而不显示上面的确认窗口,所以我使用了以下代码

I want firefox to download file automatically without showing above confirmation window, so I used the below code

FirefoxProfile firefoxProfile=new FirefoxProfile();
firefoxProfile.setPreference("browser.download.folderList",2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
firefoxProfile.setPreference("browser.download.dir",downloadPath);
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/pdf");
WebDriver driver=new FirefoxDriver(firefoxProfile); 

但Firefox仍显示相同的窗口。如何设置firefox配置文件以便自动下载PDF文件而不显示确认对话框?

but still Firefox shows the same window. How can I set firefox profile so that PDF files are downloaded automatically without showing the confirmation dialogue?

推荐答案

就像@Jason建议的那样,它很可能是另一种mime类型。
获取mime类型:

Just like @Jason suggested, it's most probably another mime type. To get the mime type:


  • 打开开发人员工具

  • 转到网络

  • 点击链接下载pdf

  • 在网络面板中,选择第一个请求

  • mime类型是响应头中的Content-Type:

  • Open Developer Tools
  • Go to Network
  • Click on the link to download the pdf
  • In the network panel, select the first request
  • The mime type is the Content-Type from the response header:

然后用Firefox下载PDF:

Then to download a PDF with Firefox:

FirefoxOptions options = new FirefoxOptions();
options.setPreference("browser.download.folderList", 2);
options.setPreference("browser.download.dir", "C:\\Windows\\temp");
options.setPreference("browser.download.useDownloadDir", true);
options.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
options.setPreference("pdfjs.disabled", true);  // disable the built-in PDF viewer

WebDriver driver = new FirefoxDriver(options);
driver.get("https://www.mozilla.org/en-US/foundation/documents");
driver.findElement(By.linkText("IRS Form 872-C")).click();

这篇关于设置Firefox配置文件以使用Selenium和Java自动下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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