在Firefox中自动下载pdf文件 [英] Auto download pdf files in Firefox

查看:658
本文介绍了在Firefox中自动下载pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Selenium和JUnit进行测试。
我试图导出报告(pdf和xls)。
使用Selenium,我点击下载链接,然后执行一个verifcar来声明文件位于正确的文件夹中。



我能够用xls,但Firefox总是问我想用PDF做什么。



Firefox,Selenium的配置是这样的:

  String downloadPath = new File(src / test / resources / firefox_download)。getAbsolutePath(); 
firefoxProfile.setPreference(browser.download.dir,downloadPath);
firefoxProfile.setPreference(browser.download.lastDir,downloadPath);
firefoxProfile.setPreference(browser.download.manager.showWhenStarting,false);
firefoxProfile.setPreference(plugin.disable_full_page_plugin_for_types,application / pdf,application / vnd.fdf);

firefoxProfile.setPreference(browser.download.folderList,2);
firefoxProfile.setPreference(browser.download.defaultFolder,downloadPath);
firefoxProfile.setPreference(browser.helperApps.neverAsk.saveToDisk,application / pdf,application / vnd.fdf,application / x-msdos-program,application / x-unknown-application-octet-stream,application /vnd.ms-powerpoint,application / excel,application / vnd.ms-publisher,application / x-unknown-message-rfc822,application / vnd.ms-excel,application / msword,application / x-mspublisher,application / x -tar,application / zip,application / x-gzip,application / x-stuffit,application / vnd.ms-works,application / powerpoint,application / rtf,application / postscript,application / x-gtar,video / quicktime,video / x-msvideo,video / mpeg,audio / x-wav,audio / x-midi,audio / x-aiff); // tipoespecífico
firefoxProfile.setPreference(browser.helperApps.alwaysAsk.force,false);

我可以手动更改,在这里:



或这里:

>



但是我需要以编程的方式来完成,否则测试将会在其他地方失败(比如服务器/声纳)
$ b

PS:Firefox 19

解决方案

您可以尝试将MIME类型设置为 application / octet-stream 浏览器不知道如何处理它,它会弹出另存为对话框


I'm doing tests with Selenium and JUnit. I'm trying to export a report (pdf and xls). With Selenium, I click the download link, and do a verifcar to assert that the file is in the correct folder.

I was able to test with the xls, but Firefox always asks what I want to do with the PDF.

The configuration of Firefox, Selenium is this:

String downloadPath = new File("src/test/resources/firefox_download").getAbsolutePath();
firefoxProfile.setPreference("browser.download.dir", downloadPath);
        firefoxProfile.setPreference("browser.download.lastDir", downloadPath);
        firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
        firefoxProfile.setPreference("plugin.disable_full_page_plugin_for_types", "application/pdf,application/vnd.fdf");

        firefoxProfile.setPreference("browser.download.folderList", 2);
        firefoxProfile.setPreference("browser.download.defaultFolder", downloadPath);
        firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf, application/vnd.fdf, application/x-msdos-program, application/x-unknown-application-octet-stream, application/vnd.ms-powerpoint, application/excel, application/vnd.ms-publisher, application/x-unknown-message-rfc822, application/vnd.ms-excel, application/msword, application/x-mspublisher, application/x-tar, application/zip, application/x-gzip,application/x-stuffit,application/vnd.ms-works, application/powerpoint, application/rtf, application/postscript, application/x-gtar, video/quicktime, video/x-msvideo, video/mpeg, audio/x-wav, audio/x-midi, audio/x-aiff"); // tipo específico
        firefoxProfile.setPreference("browser.helperApps.alwaysAsk.force", false);

I can manually change, here:

or here:

But I need to do this programmatically, or test will fail elsewhere (such as the server / sonar)

PS.: Firefox 19

解决方案

you can try to set the MIME type to application/octet-streamso if the browser doesnt know how to handle it, it will pop up a save as dialog

这篇关于在Firefox中自动下载pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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