[英]How to download XLSX file through firefox using Selenium in Java?
我正在嘗試使用以下代碼下載 xlsx 文件:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.dir", "directory where to save data");
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/x-excel, application/x-msexcel, application/excel, application/vnd.ms-excel");
ob = new FirefoxDriver(profile);
但是在顯示下載對話框后測試停止,並且沒有下載任何文件。
但是如果我通過更改上面代碼中提到的 mime 類型來嘗試對 csv 文件使用相同的代碼,那么它工作正常。
請幫我。 謝謝你。
我添加了以下代碼,而不是上面的代碼:
firefoxProfile.setPreference("browser.download.dir",dest_path);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/xls;text/csv");
現在它工作正常。 xlsx 的 MIME 類型無法正常工作,因此我嘗試為 xls 文件放置 MIME 類型,現在它工作正常。 XLSX 文件正在自動下載。
只需使用此代碼:
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
看起來您正在下載的文件的 MIME 類型不同(可能類似於application/force-download
),正如您提到的 xlsx 的正確 MIME 類型 - application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
。
安裝“HTTPFox”Firefox 插件來記錄流量並檢查其中捕獲的實際 MIME 類型。 使用該 MIME 類型更新后,您的代碼應該可以工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.