简体   繁体   English

将 Firefox 配置文件设置为使用 Selenium 和 Java 自动下载文件

[英]Set Firefox profile to download files automatically using Selenium and Java

I want to verify file download using Selenium WebDriver and Java.我想使用 Selenium WebDriver 和 Java 验证文件下载。 The file to download is of PDF format.要下载的文件为 PDF 格式。 When WebDriver clicks on "Download" link in the AUT, Firefox opens up the following download confirmation window:当 WebDriver 单击 AUT 中的“下载”链接时,Firefox 会打开以下下载确认窗口:

下载确认窗口

I want Firefox to download the file automatically without showing above confirmation window, so I used the below code:我想让 Firefox 自动下载文件而不显示上面的确认窗口,所以我使用了下面的代码:

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

but still Firefox shows the same window.但 Firefox 仍然显示相同的窗口。 How can I set Firefox profile so that PDF files are downloaded automatically without showing the confirmation dialogue?如何设置 Firefox 配置文件,以便在不显示确认对话框的情况下自动下载 PDF 文件?

Just like @Jason suggested, it's most probably another mime type.就像@Jason 建议的那样,它很可能是另一种 mime 类型。 To get the mime type:要获取 MIME 类型:

  • Open Developer Tools打开开发者工具
  • Go to Network进入网络
  • Click on the link to download the pdf点击链接下载pdf
  • In the network panel, select the first request在网络面板中,选择第一个请求
  • The mime type is the Content-Type from the response header: mime 类型是响应头中的 Content-Type:

在此处输入图片说明

Then to download a PDF with Firefox:然后使用 Firefox 下载 PDF:

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.download.viewableInternally.enabledTypes", "");
options.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf;text/plain;application/text;text/xml;application/xml");
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();

The way it currently works in Firefox 57.0b13 is它目前在 Firefox 57.0b13 中的工作方式是

FirefoxProfile profile = new FirefoxProfile();
// profile.setPreference("browser.download.useDownloadDir", true); This is true by default. Add it if it's not working without it.

profile.setPreference("browser.download.folderList",2); //Use for the default download directory the last folder specified for a download
profile.setPreference("browser.download.dir", "/Path/to/directory"); //Set the last directory used for saving a file from the "What should (browser) do with this file?" dialog.
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf"); //list of MIME types to save to disk without asking what to use to open the file
profile.setPreference("pdfjs.disabled", true);  // disable the built-in PDF viewer

firefoxOptions.setProfile(profile);

Detailed info about eachFirefox profile setting每个Firefox 配置文件设置的详细信息

If anyone is having this issue within a SPA environment, then I hit an issue where the setting the saveToDisk preference to the expected content type didn't work (in my case text/csv ).如果有人在 SPA 环境中遇到此问题,那么我会遇到一个问题, saveToDisk首选项设置为预期的内容类型不起作用(在我的情况下为text/csv )。

The reason why is the SPA UI initiates a HTTP call to the backend api to get the CSV data.原因是 SPA UI 向后端 api 发起 HTTP 调用以获取 CSV 数据。 It then does a trick to create an <A> element which it clicks to initiate the download to the local machine.然后它会创建一个<A>元素,单击该元素以启动到本地计算机的下载。 The trick creates a Blob object with the CSV data and type must be set to application/octet-stream as part of it.该技巧使用 CSV 数据创建一个Blob对象,并且类型必须设置为application/octet-stream作为其中的一部分。 Therefore the saveToDisk must also be set to application/octet-stream for this to work.因此, saveToDisk必须将saveToDisk设置为application/octet-stream才能使其工作。

I would write this as a comment, but I don't have enough reputation points--once the selenium webdriver is launched you can navigate to about:config and search for browser.helperApps.neverAsk.saveToDisk to confirm that the types you specific were properly recorded.我会把它写成评论,但我没有足够的声望点——一旦 selenium webdriver 启动,你可以导航到 about:config 并搜索 browser.helperApps.neverAsk.saveToDisk 以确认你指定的类型是正确记录。

In my case the issue was resolved by also including在我的情况下,问题也通过包括

prof.set_preference("browser.helperApps.neverAsk.openFile", "application/pdf, application/octet-stream, application/x-winzip, application/x-pdf, application/x-gzip")

In case you are like me and looking at this for other file types or multiple types.如果您像我一样并查看其他文件类型或多种类型。 Make sure you only set the neverAsk preference once.确保您只设置了neverAsk首选项一次。

opts.set_preference('browser.download.folderList', 2)
opts.set_preference('browser.download.manager.showWhenStarting', False)   opts.set_preference('browser.download.dir', str(download_directory))    opts.set_preference('browser.download.useDownloadDir', True)

# important part!
opts.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/zip')

I'll gladly update this if people know how to include more than one mime type!如果人们知道如何包含不止一种 mime 类型,我会很乐意更新这个!

Other mime types for quick reference:其他用于快速参考的 MIME 类型:

# CSV mimetype = 'text/csv'
# TXT mimetype = 'text/txt'
# EXCEL mimetype = 'application/vnd.ms-excel'

It is 2020 now.现在是 2020 年。 Find MIME type as @Florent B. mentioned above.找到上面提到的@Florent B. 的 MIME 类型。 For me, download csv file and found that Content-Type = "application/octet-stream"对我来说,下载 csv 文件,发现 Content-Type = "application/octet-stream"

To download to folder Downloads:下载到文件夹下载:

FirefoxOptions options = new FirefoxOptions();
options.addPreference("browser.download.folderList",1);
options.addPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");
WebDriver driver = new FirefoxDriver(options);

To download to desktop, change the value in 2nd line to 0:要下载到桌面,请将第 2 行中的值更改为 0:

FirefoxOptions options = new FirefoxOptions();
options.addPreference("browser.download.folderList",0);
options.addPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");
WebDriver driver = new FirefoxDriver(options);

To download to another folder:下载到另一个文件夹:

FirefoxOptions options = new FirefoxOptions();
options.addPreference("browser.download.dir", "D:\\Test");
options.addPreference("browser.download.folderList",2);
options.addPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");
WebDriver driver = new FirefoxDriver(options);

Err, according to JRodDynamite's answer:呃,根据 JRodDynamite 的回答:

opts.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/zip,text/csv,text/txt')

should do the trick for multiple mime types - in this case just for either zip, csv & txt files.应该对多种 mime 类型起作用 - 在这种情况下仅适用于 zip、csv 和 txt 文件。

Personally, (and the reason I'm on this page right now) I find it puzzling that the 'just save the #$&*!'就我个人而言,(也是我现在在这个页面上的原因)我觉得令人费解的是“只需保存 #$&*!” option is not the default for 'unknown' - aka 'application/octet-stream' - file types.选项不是 'unknown' - aka 'application/octet-stream' - 文件类型的默认值。 Oh, well, such is life!哦,好吧,这就是生活! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM