如何使用Selenium Webdriver下载excel时在firefox中处理下载弹出窗口
我试图从Firefox和Webdriver下载Excel文件,但我无法处理下载弹出窗口。
单击按钮时,我需要自动下载文件,而不显示弹出窗口。
这是我的代码:
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;
但是,上面的代码不起作用。 任何人都可以帮忙吗?
首先,您需要获取与文件对应的mime类型:
- 打开开发者工具,然后打开网络选项卡
- 返回页面并单击要下载的文件
- 返回网络面板并选择第一个请求
- 从响应头中复制Content-Type右侧的mime类型:
- 使用mime类型设置首选项“browser.helperApps.neverAsk.saveToDisk”
- 确保下载文件夹“browser.download.dir”存在
这是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();
FirefoxProfile profile = new FirefoxProfile(); // profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", " text/plain, application/octet-stream doc xls pdf txt"); profile.SetPreference("browser.download.manager.alertOnEXEOpen", false); profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/csv, text/csv, text/plain,application/octet-stream doc xls pdf txt"); profile.SetPreference("browser.download.manager.focusWhenStarting", false); profile.SetPreference("browser.download.useDownloadDir", true); profile.SetPreference("browser.helperApps.alwaysAsk.force", false); profile.SetPreference("browser.download.manager.closeWhenDone", true); profile.SetPreference("browser.download.manager.showAlertOnComplete", false); profile.SetPreference("browser.download.manager.useWindow", false); profile.SetPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false); profile.SetPreference("pdfjs.disabled", true); _driverInstance = new FirefoxDriver(profile);
这些设置对我有用。 希望它可以帮到你。