一:主要內容
- 下載效果展示
- 代碼內容展示
- saveToDisk不生效說明,即文件沒有下載下來解決辦法
二:展示效果
1.下載效果展示
用selenium3無人工干預的自動下載該文件到指定路徑下,如:D:\downloadFiles
2.代碼展示
1 package cn.gloryroad; 2 3 import org.openqa.selenium.*; 4 import org.openqa.selenium.firefox.FirefoxDriver; 5 import org.openqa.selenium.firefox.FirefoxOptions; 6 import org.openqa.selenium.firefox.FirefoxProfile; 7 import org.testng.annotations.AfterMethod; 8 import org.testng.annotations.BeforeMethod; 9 import org.testng.annotations.Test; 10 11 public class TestDemo { 12 //設定下載文件存儲的文件路徑 13 public static String downloadFilePath = "D:\\downloadFiles"; 14 WebDriver driver; 15 String baseUrl; 16 JavascriptExecutor js; 17 18 @BeforeMethod 19 public void beforeMethod() { 20 baseUrl="http://ftp.mozilla.org/pub/mozilla.org//firefox/releases/35.0b8/win32/zh-CN/"; 21 } 22 23 @AfterMethod 24 public void afterMethod() { 25 driver.quit(); 26 } 27 28 29 @Test 30 public void testdataPicker() throws Exception { 31 System.setProperty("webdriver.firefox.bin","D:\\firefox\\firefox.exe"); 32 driver = new FirefoxDriver(firefoxDriverOptions()); 33 34 driver.get(baseUrl); 35 //單擊包含“Stub”關鍵字的下載鏈接 36 driver.findElement(By.partialLinkText("Stub")).click(); 37 //設定10秒鍾的延遲,讓程序下載完成。如果網絡下載很慢,可以根據預估的下載完成時間, 38 //設定暫停時間 39 try{ 40 Thread.sleep(10000); 41 }catch(Exception e){ 42 e.printStackTrace(); 43 } 44 } 45 46 public static FirefoxOptions firefoxDriverOptions() throws Exception { 47 FirefoxOptions options = new FirefoxOptions(); 48 //聲明一個 profile 對象 49 FirefoxProfile profile = new FirefoxProfile(); 50 //設置 Firefox 的browser.download.folderList屬性為 2 51 //如果沒有進行顯示設定,則使用默認值 1,表示下載文件保存在“下載”文件夾 52 //設定為 0,則下載文件會被保存在用戶的桌面上 53 //設定為 2,則下載文件會被保存在指定的文件夾下 54 profile.setPreference("browser.download.folderList",2); 55 //browser.download.manager.showWhenStarting的屬性默認值為 true 56 //設定為 true,則在用戶啟動下載的時候顯示 Firefox 瀏覽器的文件下載窗口 57 //設定為 false,則在用戶啟動下載的時候不顯示 Firefox 瀏覽器的文件下載窗口 58 profile.setPreference("browser.download.manager.showWhenStarting",false); 59 Thread.sleep(5000); 60 //browser.download.dir設定下載文件保存的目錄 61 profile.setPreference("browser.download.dir",downloadFilePath); 62 //browser.helperApps.neverAsk.openFile表示直接打開下載文件,不顯示確認框 63 //默認值為空字符串,下行代碼行設定了多種文件的 MIME類型,例如,application/x-msdownload 64 //表示.exe類型的文件,application/excel表示 Excel 類型的文件 65 profile.setPreference("browser.helperApps.neverAsk.openFile", 66 "application/octet-stream,application/exe,text/csv, application/pdf, application/x-msexcel,application/excel,application/x-excel, application/excel,application/x-excel,application/excel,application/vnd.ms-excel,application/x-excel,application/x-msexcel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml,application/excel,application/x-msdownload"); 67 //browser.helperApps.neverAsk.saveToDisk表示下載文件是否直接保存到磁盤 68 //默認值為空字符串,下行代碼行設定了多種文件的 MIME類型,例如,application/x-msdownload 69 //表示.exe類型的文件,application/excel表示 Excel 類型的文件 70 profile.setPreference("browser.helperApps.neverAsk.saveToDisk", 71 "application/octet-stream,application/exe,text/csv,application/pdf,application/x-msexcel,application/excel,application/x-excel, application/excel,application/x-excel,application/excel, application/vnd.ms- excel,application/x-excel,application/x-msexcel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml,application/excel,text/x-c,application/x-msdownload"); 72 // browser.helperApps.alwaysAsk.force對於未知的 MIME 類型文件會彈出窗口 73 // 讓用戶處理,默認值為true,設定為false表示不會記錄打開未知 MIME 類型 74 // 文件的方式 75 profile.setPreference("browser.helperApps.alwaysAsk.force",false); 76 //下載.exe文件彈出警告,默認值是 true,設定為false 則不會彈出警告框 77 profile.setPreference("browser.download.manager.alertOnEXEOpen",false); 78 // browser.download.manager.focusWhenStarting設定下載框在下載時會獲取焦點 79 // 默認值為 true,設定為 false 表示不獲取焦點 80 profile.setPreference("browser.download.manager.focusWhenStarting",false); 81 // browser.download.manager.useWindow設定下載是否顯示下載框,默認值為true 82 // 設定為 false 會把下載框進行隱藏 83 profile.setPreference("browser.download.manager.useWindow",false); 84 // browser.download.manager.showAlertOnComplete設定下載文件結束后是否顯示下載 85 // 完成提示框,默認值為 true,設定為 false 表示下載完成后不顯示下載完成提示框 86 profile.setPreference("browser.download.manager.showAlertOnComplete",false); 87 // browser.download.manager.closeWhenDone設定下載結束后是否自動關閉下載框 88 // 默認值為true,設定為false 表示不關閉下載管理器 89 profile.setPreference("browser.download.manager.closeWhenDone",false); 90 91 options.setProfile(profile); 92 return options; 93 } 94 95 }
3.如果運行代碼后發現你的文件彈窗還是彈出來了,文件並沒有下載保存到指定的文件路徑下,那么請檢查下你的文件類型是否包含在上訴列出的文件類型之內,我之前遇到的問題是我要下載exe文件,然后給browser.helperApps.neverAsk.openFile和browser.helperApps.neverAsk.saveToDisk都加入了application/exe類型,但是運行代碼后發現,保存文件的彈窗照樣還是打開了,文件並沒有被下載下來。
這個時候你就可以抓包或者通過瀏覽器的f12工具看下你下載文件的那個請求的響應數據的Content-Type是什么類型的,加入進去就好,如我上面的exe文件,用f12工具查看它的content-type類型為:application/x-msdownload
然后我在我的代碼里設置的browser.helperApps.neverAsk.openFile和browser.helperApps.neverAsk.saveToDisk里加入該項application/x-msdownload即可,文件就能被下載成功了,其他文件類型按照同樣的方法也可以解決。