如何使用Selenium Webdriver下载excel时在firefox中处理下载弹出窗口 [英] How to handle download pop-up in firefox, while downloading excel using Selenium Webdriver
本文介绍了如何使用Selenium Webdriver下载excel时在firefox中处理下载弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从Firefox和Webdriver下载Excel文件,但我无法处理下载弹出窗口。
I am trying to download an Excel file from Firefox and Webdriver, but i can't handle the download pop-up.
点击按钮时我需要文件自动下载,不显示弹出窗口。
When click on button i need the file to download automatically, without showing pop-up.
这是我的代码:
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.download.folderList", 2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
firefoxProfile.setPreference("browser.download.dir", Constant.Downloaded_Path);
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv/xls/xlsx");
firefoxProfile.setPreference("browser.helperApps.neverAsk.openFile",
"text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
firefoxProfile.setPreference("browser.helperApps.alwaysAsk.force", false);
firefoxProfile.setPreference("browser.download.manager.alertOnEXEOpen", false);
firefoxProfile.setPreference("browser.download.manager.focusWhenStarting", false);
firefoxProfile.setPreference("browser.download.manager.useWindow", false);
firefoxProfile.setPreference("browser.download.manager.showAlertOnComplete", false);
firefoxProfile.setPreference("browser.download.manager.closeWhenDone", false);
return firefoxProfile;
但是,上面的代码无效。任何人都可以帮忙吗?
But, the above code is not working. Can any one help?
推荐答案
首先你需要得到与文件对应的mime类型:
First you need to get the mime type corresponding to the file:
- 打开开发人员工具,然后打开网络选项卡
- 返回页面并单击要下载的文件
- 返回网络面板并选择第一个请求
- 从响应标题中复制Content-Type右侧的mime类型:
- Open Developer Tools and then the Network tab
- Go back to the page and click on the file to download
- Go back to the network panel and select the first request
- Copy the mime type on the right of Content-Type from the response header:
- 使用您的mime类型设置首选项browser.helperApps.neverAsk.saveToDisk
- 确保下载文件夹browser.download.dir存在
以下是Firefox的一个工作示例:
Here is a working example with Firefox:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", "C:\\Windows\\temp");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.ms-excel");
profile.setPreference("pdfjs.disabled", true); // disable the built-in PDF viewer
WebDriver driver = new FirefoxDriver(profile);
driver.get("http://www.exinfm.com/free_spreadsheets.html");
driver.findElement(By.linkText("Capital Budgeting Analysis")).click();
这篇关于如何使用Selenium Webdriver下载excel时在firefox中处理下载弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文