如何使用 webdriver 在 firefox 中处理下载 .xlsx 文件,其中窗口弹出窗口默认为“打开方式”单选按钮而不是“保存文件”

Vis*_*tap 2 java firefox popupwindow savefiledialog selenium-webdriver

我有一个带有链接的 Web 应用程序。单击此链接时,它会打开弹出窗口,要求使用特定应用程序打开文件。

我编写了以下代码来创建 firefox 配置文件,它适用于其他文件扩展名,但不适用于 .xlsx 文件。

        profile.setEnableNativeEvents(true);

        profile.setPreference("browser.download.folderList", 2);
        profile.setPreference("browser.download.manager.showWhenStarting", false);
        profile.setPreference("browser.download.dir", System.getProperty("user.dir")+"\\src\\com\\Download");
        profile.setPreference("browser.download.downloadDir", System.getProperty("user.dir")+"\\src\\com\\Download");
        profile.setPreference("browser.download.defaultFolder", System.getProperty("user.dir")+"\\src\\com\\Download");
        profile.setPreference("browser.download.manager.closeWhenDone", true);
        profile.setPreference("pdfjs.disabled", true);
        profile.setPreference("browser.helperApps.alwaysAsk.force", false);
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/xls, application/zip,text/csv,application/msword,application/excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/pdf," +
                "application/vnd.ms-excel,application/msword,application/unknown,application/vnd.openxmlformats-officedocument.wordprocessingml.document");
Run Code Online (Sandbox Code Playgroud)

下面是我得到的弹出窗口:- 弹出窗口

  • 我认为,由于窗口弹出窗口未默认为“保存文件”单选按钮,因此 Firefox 配置文件不起作用。任何解决方案来处理这种情况?

小智 5

这个偏好对我有用。

profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
Run Code Online (Sandbox Code Playgroud)

下表记录了处理 Office 2007 文档时可用的 HTTP MIME 类型:

扩展 MIME 类型

  • .doc 应用程序/msword
  • .dot 应用程序/msword
  • .docx 应用程序/vnd.openxmlformats-officedocument.wordprocessingml.document
  • .dotx 应用程序/vnd.openxmlformats-officedocument.wordprocessingml.template
  • .docm 应用程序/vnd.ms-word.document.macroEnabled.12
  • .dotm 应用程序/vnd.ms-word.template.macroEnabled.12
  • .xls 应用程序/vnd.ms-excel
  • .xlt 应用程序/vnd.ms-excel
  • .xla 应用程序/vnd.ms-excel
  • .xlsx 应用程序/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  • .xltx 应用程序/vnd.openxmlformats-officedocument.spreadsheetml.template
  • .xlsm 应用程序/vnd.ms-excel.sheet.macroEnabled.12
  • .xltm 应用程序/vnd.ms-excel.template.macroEnabled.12
  • .xlam 应用程序/vnd.ms-excel.addin.macroEnabled.12
  • .xlsb 应用程序/vnd.ms-excel.sheet.binary.macroEnabled.12
  • .ppt 应用程序/vnd.ms-powerpoint
  • .pot 应用程序/vnd.ms-powerpoint
  • .pps 应用程序/vnd.ms-powerpoint
  • .ppa 应用程序/vnd.ms-powerpoint
  • .pptx 应用程序/vnd.openxmlformats-officedocument.presentationml.presentation
  • .potx 应用程序/vnd.openxmlformats-officedocument.presentationml.template
  • .ppsx 应用程序/vnd.openxmlformats-officedocument.presentationml.slideshow
  • .ppam 应用程序/vnd.ms-powerpoint.addin.macroEnabled.12
  • .pptm 应用程序/vnd.ms-powerpoint.presentation.macroEnabled.12
  • .potm 应用程序/vnd.ms-powerpoint.template.macroEnabled.12
  • .ppsm 应用程序/vnd.ms-powerpoint.slideshow.macroEnabled.12