将 Firefox 配置文件设置为使用 Selenium 和 Java 自动下载文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36309314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set Firefox profile to download files automatically using Selenium and Java
提问by stackoverflow
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 opens up the following download confirmation window:
我想使用 Selenium WebDriver 和 Java 验证文件下载。要下载的文件为 PDF 格式。当 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. How can I set Firefox profile so that PDF files are downloaded automatically without showing the confirmation dialogue?
但 Firefox 仍然显示相同的窗口。如何设置 Firefox 配置文件,以便在不显示确认对话框的情况下自动下载 PDF 文件?
采纳答案by Florent B.
Just like @Jason suggested, it's most probably another mime type. To get the mime type:
就像@Jason 建议的那样,它很可能是另一种 mime 类型。要获取 MIME 类型:
- 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:
- 打开开发者工具
- 进入网络
- 点击链接下载pdf
- 在网络面板中,选择第一个请求
- 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.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();
回答by 7cart project
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 each Firefox profile setting
每个Firefox 配置文件设置的详细信息
回答by deejbee
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. It then does a trick to create an <A>
element which it clicks to initiate the download to the local machine. The trick creates a Blob
object with the CSV data and type must be set to application/octet-stream
as part of it. Therefore the saveToDisk
must also be set to application/octet-stream
for this to work.
原因是 SPA UI 向后端 api 发起 HTTP 调用以获取 CSV 数据。然后<A>
,它会创建一个元素,单击该元素以启动对本地计算机的下载。该技巧Blob
使用 CSV 数据创建一个对象,并且类型必须设置application/octet-stream
为其中的一部分。因此,saveToDisk
还必须设置application/octet-stream
为使其工作。
回答by Bjc
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")
回答by Daniel Butler
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'