Firefox 版本是72
geckodriver 是 0.24
selenium 是3.14
代碼中注釋有關於FirefoxOptions,FirefoxProfile的解釋,請各位尋找一下,不做另外解釋
import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; import org.openqa.selenium.firefox.FirefoxProfile; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class TestDemo { //設定存儲下載文件的路徑 public static String downloadFilePath = "C:\\downloadFiles"; WebDriver driver; String baseUrl; JavascriptExecutor js; @BeforeMethod public void befordMethod(){ System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe"); System.setProperty("webdriver.gecko.driver", "C:\\Users\\Administrator\\Desktop\\geckodriver.exe"); //baseUrl = "http://ftp.mozilla.org/pub/firefox/releases/56.0/win64/zh-CN/"; baseUrl = "http://ftp.mozilla.org/pub/firefox/releases/35.0b8/win32/zh-CN/"; } @Test public void testDownloadFile() throws Exception{ driver = new FirefoxDriver(firefoxDriverOptions()); //driver = new FirefoxDriver(); driver.get(baseUrl); //單機包含 Stub關鍵字的下載鏈接 driver.findElement(By.partialLinkText("Stub")).click(); //driver.findElement(By.xpath("/html/body/table/tbody/tr[3]/td[2]/a")).click(); //設定10秒延遲,讓程序下載完成,如果網絡下載很慢,可以根據預估的下載完成時間 //設定暫停時間 //TODO 是否可以 用一個循環,來判定是否 下載完成,找到下載的元素,是否出現“已下載”,如果出現這個 partialLinkText 那么久break try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static FirefoxOptions firefoxDriverOptions(){ try { Thread.sleep(4000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } FirefoxOptions options = new FirefoxOptions(); //聲明一個profile對象 FirefoxProfile profile = new FirefoxProfile(); //設置Firefox的“broswer.download.folderList”屬性為2 /** * 如果沒有進行設定,則使用默認值 1,表示下載文件保存在“下載”文件夾中 * 設定為0,則下載文件會被保存在用戶的桌面上 * 設定為2,則下載的文件會被保存的用戶指定的文件夾中 */ profile.setPreference("browser.download.folderList", 2); //browser.download.manager.showWhenStarting的屬性默認值為true //設定為 true , 則在用戶啟動下載時顯示Firefox瀏覽器的文件下載窗口 //設定為false,則在用戶啟動下載時不顯示Firefox瀏覽器的文件下載窗口 profile.setPreference("browser.download.manager.showWhenStarting", false); //設定文件保存的目錄 profile.setPreference("browser.download.dir", downloadFilePath); //browser.helperApps.neverAsk.openFile 表示直接打開下載文件,不顯示確認框 //默認值.exe類型的文件,"application/excel"表示Excel類型的文件 // application/x-msdownload profile.setPreference("browser.helperApps.neverAsk.openFile", "application/x-msdownload"); //browser.helperApps.never.saveToDisk 設置是否直接保存 下載文件到磁盤中默認值為空字符串,廈航代碼行設定了多種溫江的MIME類型 profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/x-msdownload"); //browser.helperApps.alwaysAsk.force 針對位置的MIME類型文件會彈出窗口讓用戶處理,默認值為true ,設定為false 表示不會記錄打開未知MIME類型文件 profile.setPreference("browser.helperApps.alwaysAsk.force", true); //下載.exe文件彈出窗口警告,默認值是true ,設定為false 則不會彈出警告框 profile.setPreference("browser.download.manager.alertOnEXEOpen", false); //browser.download.manager.focusWhenStarting設定下載框在下載時會獲取焦點 profile.setPreference("browser.download.manager.focusWhenStarting", true); //browser.download.manager.useWindow 設定下載是否現在下載框,默認值為true,設定為false 會把下載框隱藏 profile.setPreference("browser.download.manager.useWindow", false); //browser.download.manager.showAlertOnComplete 設定下載文件結束后是否顯示下載完成的提示框,默認值為true, //設定為false表示下載完成后,現在下載完成提示框 profile.setPreference("browser.download.manager.showAlertOnComplete", false); //browser.download.manager.closeWhenDone 設定下載結束后是否自動關閉下載框,默認值為true 設定為false 表示不關閉下載管理器 profile.setPreference("browser.download.manager.closeWhenDone", false); try { Thread.sleep(80000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } options.setProfile(profile); return options; } @AfterMethod public void afterMethod(){ try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } driver.quit(); }