一、Driver下載地址:
http://docs.seleniumhq.org/download/
二、啓動firefox瀏覽器(不需要下載驅動,原生支持)
1、firefox安裝在默認路徑下:
//啓動默認安裝路徑下的ff
public void StartFireFoxByDefault(){
System.out.println("start firefox browser...");
WebDriver driver = new FirefoxDriver();
Navigation navigation = driver.navigate();
navigation.to("http://www.baidu.com/");
System.out.println("start firefox browser succeed...");
}
2、firefox未安裝在默認路徑下:
public static void StartFireFoxNotByDefault(){
System.out.println("start firefox browser...");
System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla Firefox/firefox.exe");
WebDriver driver = new FirefoxDriver();
Navigation navigation = driver.navigate();
navigation.to("http://www.baidu.com/");
System.out.println("start firefox browser succeed...");
}
3、啓動firefox時加載插件:
首先,要知道我們爲什麼需要加載插件?原因是webdriver在啓動瀏覽器時,啓動的一個乾淨的沒有任務、插件及cookies信息的瀏覽器(即使你本機的firefox安裝了某些插件,webdriver啓動firefox也是沒有這些插件的),但是有可能被測系統本身需要插件或者需要調試等等,此時可以用如下方法在啓動firefox時加載插件,下面示例加載firebug插件:
public static void StartFireFoxLoadPlugin(){
System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla Firefox/firefox.exe");
File file = new File("files/firebug-2.0.7-fx.xpi");
FirefoxProfile profile = new FirefoxProfile();
try {
profile.addExtension(file);
} catch (IOException e) {
e.printStackTrace();
}
profile.setPreference("extensions.firebug.currentVersion", "2.0.7");
//active firebug extensions
profile.setPreference("extensions.firebug.allPagesActivation", "on");
WebDriver driver = new FirefoxDriver(profile);
driver.get("http://www.baidu.com");
System.out.println("start firefox browser succeed...");
}
4、啓動firefox時設置profile:
上面提到過webdriver啓動firefox時是啓動一個完全新的瀏覽器,我們除了可以使用上面提到的方法定製插件,webdriver還可以對profile進行定製(在firefox地址欄中輸入about:config,可以查看firefox的參數),下面設置代理和默認下載路徑:
public static void StartFireFoxByProxy(){
String proxyIp = "10.17.171.11";
int proxyPort = 8080;
System.out.println("start firefox browser...");
System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla Firefox/firefox.exe");
FirefoxProfile profile = new FirefoxProfile();
//設置代理參數
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.http", proxyIp);
profile.setPreference("network.proxy.http_port", proxyPort);
//設置默認下載路徑
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", "D:\\");
WebDriver driver = new FirefoxDriver(profile);
driver.get("http://www.baidu.com");
System.out.println("start firefox browser succeed...");
}
5、啓動本機器的firefox配置:
每次啓動如果都像上面那樣在代碼裏面配置profile比較麻煩,可以使用下面的方法啓動本機器的firefox的配置,換句話說就是我們可以事先配置本機的firefox然後用webdriver啓動它,這樣本機上的firefox安裝了什麼插件都可以直接使用了,不需要在配置profile:
public static void StartLocalFirefox(){
System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla Firefox/firefox.exe");
ProfilesIni pi = new ProfilesIni();
FirefoxProfile profile = pi.getProfile("default");
WebDriver driver = new FirefoxDriver(profile);
driver.get("http://www.baidu.com/");
}
6、如果在機器B上要啓動機器A上的firefox配置,可以先導出A的配置,然後加載:
1、將A機器上的Profiles文件夾”C:\Users\cloudchen\AppData\Local\Mozilla\Firefox\Profiles”給拷貝出來到某個目錄
2、代碼:
public static void StartFireFoxByOtherConfig(){
System.out.println("start firefox browser...");
System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla Firefox/firefox.exe");
File file = new File("files\\lg6mie1i.default");//profiles文件目錄,這裏我是放在工程目錄下的files文件夾下
FirefoxProfile profile = new FirefoxProfile(file);
WebDriver driver = new FirefoxDriver(profile);
driver.get("http://www.baidu.com");
System.out.println("start firefox browser succeed...");
}
PS:如果插件或其它東東未加載成功,可以檢查下profile文件夾下是否包含插件信息。
三、啓動chrome瀏覽器
1、啓動chrome需要chromedriver的驅動:
public static void StartChrome(){
System.out.println("start firefox browser...");
System.setProperty("webdriver.chrome.driver", "files\\chromedriver.exe");//指定驅動路徑
WebDriver driver = new ChromeDriver();
driver.get("http://www.baidu.com/");
System.out.println("start firefox browser succeed...");
}
另,如果不想用setProperty的方式,可以將chromedriver.exe 放在”C:\Windows\System32”路徑下或者path可以找到的路徑下並重啓電腦即可。
2、加載插件:
public static void StartChromeLoadPlugin(){
System.out.println("start firefox browser...");
System.setProperty("webdriver.chrome.driver", "files\\chromedriver.exe");
File file = new File ("files\\youtube.crx");
ChromeOptions options = new ChromeOptions();
options.addExtensions(file);
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.baidu.com/");
System.out.println("start firefox browser succeed...");
}
四、啓動IE瀏覽器
1、IE啓動和chrome類似也需要下載相應的驅動:
public static void StartIE(){
System.out.println("start firefox browser...");
System.setProperty("webdriver.ie.driver", "files\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("http://www.baidu.com/");
System.out.println("start firefox browser succeed...");
}
2、IE下沒有插件加載
3、IE的放大比例爲要設置100%
4、啓動IE時,需關閉如下圖中4個區域的保護模式:
5、對於第4點提到的關閉保護模式,還可以使用代碼關閉:
//啓動IE瀏覽器並關閉保護模式
public static void StartIEAndCloseProtectedMode(){
System.out.println("start firefox browser...");
System.setProperty("webdriver.ie.driver", "files\\IEDriverServer.exe");
DesiredCapabilities dc = DesiredCapabilities.internetExplorer();
dc.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
//IE默認啓動保護模式,要麼手動在瀏覽器的設置中關閉保護模式,要麼在代碼中加上這一句,即可
dc.setCapability("ignoreProtectedModeSettings", true);
WebDriver driver = new InternetExplorerDriver(dc);
driver.get("http://www.baidu.com/");
System.out.println("start firefox browser succeed...");
}
感謝作者的分享,原文路徑:http://www.cnblogs.com/puresoul/p/4251536.html