启动Firefox Browser。
创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站制作、做网站、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的元氏网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
1这种情况适用于Firefox安装在了默认路径下
WebDriver driver = new FirefoxDriver();//直接new一个FirefoxDriver
Navigation navigation = driver.navigate();
// 进入百度首页
navigation.to("http://www.baidu.com");
2 这种情况适用于Firefox未安装在默认路径下
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的插件。
首先要知道我们为什么需要加载插件原因是webdriver在启动浏览器时启动的一个干净的没有任务、插件及cookies信息的浏览器(即使你本机的firefox安装了某些插件webdriver启动firefox也是没有这些插件的)但是有可能被测系统本身需要插件或者需要调试等等此时可以用如下方法在启动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...");
}
}
--------------------------------------
上述代码并未调通报如下异常
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.
at org.openqa.selenium.firefox.FirefoxDriver.
at org.openqa.selenium.firefox.FirefoxDriver.
at org.openqa.selenium.firefox.FirefoxDriver.
at org.openqa.selenium.firefox.FirefoxDriver.
at TestDemo.main(TestDemo.java:27)
Caused by: org.openqa.selenium.firefox.UnableToCreateProfileException: java.io.FileNotFoundException: \files\firebug-2.0.7-fx.xpi (系统找不到指定的路径。)
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 (系统找不到指定的路径。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.
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)种情况未调通。
每次启动如果都像上面那样在代码里面配置profile比较麻烦可以使用下面的方法启动本机器的firefox的配置换句话说就是我们可以事先配置本 机的firefox然后用webdriver启动它这样本机上的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官网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();
}
新闻名称:WebDriver总结-不同浏览器的启动方式
当前链接:http://lswzjz.com/article/ishjjd.html