簡體   English   中英

使用Selenium Webdriver下載excel時如何處理firefox中的下載彈出窗口

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

我正在嘗試從 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); 

這些設置對我有用。 希望它可以幫助你。

您需要為要下載的應用程序提供 mimetypes。 對於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