简体   繁体   English

使用Selenium Webdriver下载excel时如何处理firefox中的下载弹出窗口

[英]How to handle download pop-up in firefox, while downloading excel using Selenium Webdriver

I am trying to download an Excel file from Firefox and Webdriver, but i can't handle the download pop-up.我正在尝试从 Firefox 和 Webdriver 下载 Excel 文件,但无法处理下载弹出窗口。

When click on button i need the file to download automatically, without showing pop-up.单击按钮时,我需要自动下载文件,而不显示弹出窗口。

Here is my code:这是我的代码:

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?任何人都可以帮忙吗?

First you need to get the mime type corresponding to the file:首先需要获取文件对应的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:从响应头中复制 Content-Type 右侧的 MIME 类型:

在此处输入图片说明

  • Set the preference "browser.helperApps.neverAsk.saveToDisk" with your mime type使用您的 mime 类型设置首选项“browser.helperApps.neverAsk.saveToDisk”
  • Make sure the download folder "browser.download.dir" exists确保下载文件夹“browser.download.dir”存在

Here is a working example with Firefox:这是 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); 

These settings worked for me.这些设置对我有用。 Hope it might help you.希望它可以帮助你。

you need to provide mimetypes for the application you want to download.您需要为要下载的应用程序提供 mimetypes。 For python, if you want to enable it for all applications then use the following code对于python,如果要为所有应用程序启用它,请使用以下代码

import mimetypes
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", ','.join(list(it for it in mimetypes.types_map.values())))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM