下載文件
WebDriver 允許我們設置默認的文件下載路徑。也就是說文件會自動下載並且存在設置的那個目錄
中。下面以FireFox 為例執行文件的下載。
package com.mypro.jase; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxProfile; public class download { public static void main(String[] args){ FirefoxProfile firefoxProfile = new FirefoxProfile(); firefoxProfile.setPreference("browser.download.folderList",2); //設置成0 代表下載到瀏覽器默認下載路徑;設置成2 則可以保存到指定目錄。 firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false); //是否顯示開始,Ture 為顯示,Flase 為不顯示。 firefoxProfile.setPreference("browser.download.dir","d:\\java"); //用於指定你所下載文件的目錄。
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream");
//指定要下載頁面的Content-type 值,“application/octet-stream”為文件的類型 WebDriver driver = new FirefoxDriver(firefoxProfile); driver.get("https://pypi.Python.org/pypi/selenium"); driver.findElement(By.partialLinkText("selenium-2")).click(); } }