首页 > 编程语言 > 详细

Java编程语言下Selenium驱动各个浏览器代码

时间:2018-05-02 18:55:48      阅读:188      评论:0      收藏:0      [点我收藏+]

这里采用的是Selenium3.7版本,首先介绍的是在Windows环境下运行的;

总结下注意事项:

1,设置各个浏览器的Driver路径

System.setProperty("","");

2,创建一个浏览器对象

WebDriver driver = new xxx();

 

 

1. 驱动IE浏览器

IE浏览器的驱动有32位和64位,两个版本,建议使用32的IEdriver,因为64位的IE driver 跑起来实在太慢了

 1 package base;
 2 
 3 import java.util.concurrent.TimeUnit;
 4 import org.openqa.selenium.WebDriver;
 5 import org.openqa.selenium.ie.InternetExplorerDriver;
 6 
 7 public class TestIEDriver {
 8 
 9     public static void main(String args[]) throws InterruptedException {
10 
11         System.setProperty("webdriver.ie.driver", ".\\Tools\\IEDriverServer.exe");
12         WebDriver driver = new InternetExplorerDriver();
13         driver.manage().window().maximize();
14         driver.get("http://www.baidu.com");
15         String s = driver.getTitle();
16         System.out.print(s);
17         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
18         Thread.sleep(1000);
19         driver.close();
20 
21     }
22 }

 

2. 驱动Chrome浏览器

 1 package base;
 2 
 3 import java.util.Arrays;
 4 import java.util.concurrent.TimeUnit;
 5 
 6 import org.openqa.selenium.WebDriver;
 7 import org.openqa.selenium.chrome.ChromeDriver;
 8 import org.openqa.selenium.chrome.ChromeOptions;
 9 import org.openqa.selenium.remote.DesiredCapabilities;
10 
11 public class TestChromeDriver {
12 
13     public static void main(String args[]) throws InterruptedException {
14 
15         System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe");
16         ChromeOptions options = new ChromeOptions();
17         DesiredCapabilities capabilities = DesiredCapabilities.chrome();
18         capabilities.setCapability("chrome.switches", Arrays.asList("--start-maximized"));
19         options.addArguments("--test-type", "--start-maximized");
20         WebDriver driver = new ChromeDriver(options);
21         driver.get("http://www.baidu.com");
22 
23         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
24         Thread.sleep(1000);
25         driver.close();
26 
27     }
28 
29 }

 

3. 驱动火狐FireFox浏览器

 1 package base;
 2 
 3 import java.util.concurrent.TimeUnit;
 4 
 5 import org.openqa.selenium.WebDriver;
 6 import org.openqa.selenium.firefox.FirefoxDriver;
 7 
 8 public class TestFireFoxDriver {
 9 
10     public static void main(String args[]) throws InterruptedException {
11 
12         System.setProperty("webdriver.Firefox.driver", ".\\Tools\\geckodriver.exe");
13         WebDriver driver = new FirefoxDriver();
14         driver.manage().window().maximize();
15         driver.get("http://www.baidu.com");
16         String s = driver.getTitle();
17         System.out.print(s);
18         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
19         Thread.sleep(1000);
20         driver.close();
21 
22     }
23 
24 }

 

Java编程语言下Selenium驱动各个浏览器代码

原文:https://www.cnblogs.com/qianjinyan/p/8981624.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!