如何配置WebDriver启动的Firefox,让PDF在弹窗iframe中显示而非自动下载
解决Firefox 46 WebDriver中PDF自动下载而非在iframe弹窗显示的问题
你的配置里有几个关键设置导致PDF被自动下载,咱们来针对性调整:
问题根源
你当前把application/pdf加入了browser.helperApps.neverAsk.saveToDisk偏好项,这会直接告诉Firefox“遇到PDF就自动下载,别问我”,这就是PDF跳过弹窗iframe直接下载的核心原因。同时你也没把PDF类型加入允许直接打开的列表,需要补充调整。
具体修改步骤
- 从
browser.helperApps.neverAsk.saveToDisk中移除application/pdf - 将
application/pdf添加到browser.helperApps.neverAsk.openFile中 - 保留已有的PDF.js启用配置,确保内置查看器正常工作
修改后的完整配置代码
FirefoxProfile fprofile = new FirefoxProfile(); String path = System.getProperty("user.dir"); fprofile.setPreference("browser.download.dir", "D:\\AutomationFramework\\ResultFiles"); fprofile.setPreference("browser.download.folderList", 2); // 移除application/pdf,取消自动下载规则 fprofile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/x-excel, application/x-msexcel, application/excel, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/xls, text/csv, multipart/x-zip,application/zip,application/x-zip-compressed,application/x-compressed,image/jpeg,image/png,image/svg+xml"); // 添加application/pdf,允许浏览器直接打开该类型文件 fprofile.setPreference("browser.helperApps.neverAsk.openFile","application/pdf, application/xls, multipart/x-zip,application/zip,application/x-zip-compressed,application/x-compressed,image/jpeg,image/png,image/svg+xml"); fprofile.setPreference( "browser.download.manager.showWhenStarting", false ); fprofile.setPreference( "pdfjs.disabled", false ); fprofile.setPreference("plugin.scan.plid.all",false); fprofile.setPreference("plugin.scan.Acrobat","99.0"); fprofile.setPreference("plugin.disable_full_page_plugin_for_types","application/pdf;application/x-excel, application/x-msexcel, application/excel, application/vnd.ms-excel,application/xls, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
补充说明
pdfjs.disabled=false已经确保Firefox 46的内置PDF查看器(PDF.js)处于启用状态,这是在iframe中渲染PDF的基础- 移除PDF类型的自动下载规则后,浏览器会优先尝试用内置查看器打开文件,而不是直接下载
- 把PDF加入
neverAsk.openFile是为了避免浏览器弹出“打开/保存”的询问框,让流程更顺畅
调整完这些配置后,再运行脚本点击图标,弹窗里的iframe应该就能正常加载显示PDF文件了。
内容的提问来源于stack exchange,提问作者SanjayDVG
相关产品推荐
相关产品推荐

