Selenium2浏览器启动及配置

启动浏览器

  SELENIUM2在启动浏览器时,都是启动一个干净的没有任何插件及cookies信息的浏览器;

 
1     public static void myfirefox()
2     {
3         System.setProperty("webdriver.firefox.bin","D:/Mozilla Firefox/firefox.exe");
4         WebDriver driver=new FirefoxDriver();
5         Navigation navigation=driver.navigate();
6         navigation.to("http://www.baidu.com");
7     }
View Code

  ——注:如果浏览器安装时为默认路径安装,则可以去掉:System.setProperty的内容;

启动firefox(加相关插件)

 1     public static void myfirefoxplug()
 2     {
 3         //加入firefox插件
 4         File file=new File("files/firebug-2.0.6.xpi");
 5         FirefoxProfile firefoxprofile=new FirefoxProfile();
 6         
 7         //捕捉加载firefox的异常
 8         try{
 9             firefoxprofile.addExtension(file);
10         }
11         catch(IOException e)
12         {
13             e.printStackTrace();
14         }
15         //WebDriver driver=new FirefoxDriver(firefoxProfile);
16         //设置firebug的版本号
17         firefoxprofile.setPreference("extensions.firebug.currentVersion", "2.0.6");
18         //非默认路径下修改路径
19         System.setProperty("webdriver.firefox.bin","D:/Mozilla Firefox/firefox.exe");
20         
21         //实例化一个driver对象
22         WebDriver driver=new FirefoxDriver(firefoxprofile);
23         
24         //跳转实例
25         Navigation navigation=driver.navigate();
26         navigation.to("http://www.baidu.com");
27 
28     }
View Code

启动chrome

1     public static void mychrome()
2     {
3         System.setProperty("webdriver.chrome.driver", "files/chromedriver.exe");
4         WebDriver driver=new ChromeDriver();
5         Navigation navigation=driver.navigate();
6         navigation.to("http://www.sina.com");
7         
8     }
View Code

启动IE

1     public static void myie()
2     {
3         System.setProperty("webdriver.ie.driver", "files/IEDriverServer.exe");
4         WebDriver driver=new InternetExplorerDriver();
5         Navigation navigation=driver.navigate();
6         navigation.to("http://www.sohu.com");
7         
8     }
View Code

插件下载地址如下:http://pan.baidu.com/s/1pJy3Dc3

原文地址:https://www.cnblogs.com/leoliyue/p/4876731.html