您好,登錄后才能下訂單哦!
啟動Firefox Browser。
1這種情況適用于Firefox安裝在了默認(rèn)路徑下
WebDriver driver = new FirefoxDriver();//直接new一個FirefoxDriver
Navigation navigation = driver.navigate();
// 進(jìn)入百度首頁
navigation.to("http://www.baidu.com");
2 這種情況適用于Firefox未安裝在默認(rèn)路徑下
System.out.println("start firefox browser...");
System.setProperty("webdriver.firefox.bin", //指定firefox的安裝路徑
"D:/Program Files/Mozilla Firefox/firefox.exe");
WebDriver driver = new FirefoxDriver();
Navigation navigation = driver.navigate();
navigation.to("http://www.baidu.com/");
3這種情況可以加載出Firefox的插件。
首先要知道我們?yōu)槭裁葱枰虞d插件原因是webdriver在啟動瀏覽器時啟動的一個干凈的沒有任務(wù)、插件及cookies信息的瀏覽器(即使你本機(jī)的firefox安裝了某些插件webdriver啟動firefox也是沒有這些插件的)但是有可能被測系統(tǒng)本身需要插件或者需要調(diào)試等等此時可以用如下方法在啟動firefox時加載插件下面示例加載firebug插件
import java.io.File;
import java.io.IOException;
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
public class TestDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("start firefox browser...");
System.setProperty("webdriver.firefox.bin",
"C:/Program Files (x86)/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...");
}
}
--------------------------------------
上述代碼并未調(diào)通報如下異常
start firefox browser...
Exception in thread "main" org.openqa.selenium.WebDriverException: Failed to connect to binary FirefoxBinary(C:\Program Files (x86)\Mozilla Firefox\firefox.exe) on port 7055; process output follows:
null
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'
System info: host: 'XL-20150414QGDQ', ip: '192.168.80.6', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_80'
Driver info: driver.version: FirefoxDriver
at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:128)
at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:271)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:119)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:218)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:211)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:207)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:124)
at TestDemo.main(TestDemo.java:27)
Caused by: org.openqa.selenium.firefox.UnableToCreateProfileException: java.io.FileNotFoundException: \files\firebug-2.0.7-fx.xpi (系統(tǒng)找不到指定的路徑。)
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'
System info: host: 'XL-20150414QGDQ', ip: '192.168.80.6', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_80'
Driver info: driver.version: FirefoxDriver
at org.openqa.selenium.firefox.FirefoxProfile.layoutOnDisk(FirefoxProfile.java:427)
at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:85)
... 7 more
Caused by: java.io.FileNotFoundException: \files\firebug-2.0.7-fx.xpi (系統(tǒng)找不到指定的路徑。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at org.openqa.selenium.firefox.internal.FileExtension.obtainRootDirectory(FileExtension.java:80)
at org.openqa.selenium.firefox.internal.FileExtension.writeTo(FileExtension.java:59)
at org.openqa.selenium.firefox.FirefoxProfile.installExtensions(FirefoxProfile.java:443)
at org.openqa.selenium.firefox.FirefoxProfile.layoutOnDisk(FirefoxProfile.java:421)
... 8 more
4用第(3)種情況未調(diào)通。
每次啟動如果都像上面那樣在代碼里面配置profile比較麻煩可以使用下面的方法啟動本機(jī)器的firefox的配置換句話說就是我們可以事先配置本 機(jī)的firefox然后用webdriver啟動它這樣本機(jī)上的firefox安裝了什么插件都可以直接使用了不需要在配置profile:
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("start firefox browser...");
System.setProperty("webdriver.firefox.bin", "C:/Program Files (x86)/Mozilla Firefox/firefox.exe");
ProfilesIni pi = new ProfilesIni();
FirefoxProfile profile = pi.getProfile("default");
WebDriver driver = new FirefoxDriver(profile);
driver.get("http://www.baidu.com");
System.out.println("start firefox browser succeed...");
}
啟動IE Browser。
PS:除Firefox已自帶外其他瀏覽器均需從Selenium官網(wǎng)http://docs.seleniumhq.org/download/下載各自的Driver。
1啟動本地IE Browser。
System.setProperty("webdriver.ie.driver",
"E:\\selenium\\IEDriverServer.exe");//IEDriverServer.exe所在本地路徑
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
WebDriver driver = new InternetExplorerDriver(ieCapabilities);
driver.get("http://www.baidu.com");
啟動Chrome Browser。
1啟動本地Chrome Browser。
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver",
"E:\\selenium\\chromedriver.exe");//chromedriver.exe所在本地路徑
WebDriver driver = new ChromeDriver();
driver.get("http://www.baidu.com");
driver.findElement(By.id("kw")).sendKeys(Keys.chord(Keys.SHIFT,"webdriver"));
driver.findElement(By.id("su")).click();
driver.close();
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。