64位32位 java 浏览器_Java编程语言下 Selenium 驱动各个浏览器代码

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

总结下注意事项:

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

System.setProperty("","");

2,创建一个浏览器对象

WebDriver driver = new xxx();

1. 驱动IE浏览器

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

注意IE的保护模式,在各个状态下,全部禁用。如下截图,4个模式下,保护模式全部不勾选。

3867e9eaaf7e0dbc1023c0c43c0e27c4.png

1 packagebase;2

3 importjava.util.concurrent.TimeUnit;4 importorg.openqa.selenium.WebDriver;5 importorg.openqa.selenium.ie.InternetExplorerDriver;6

7 public classTestIEDriver {8

9 public static void main(String args[]) throwsInterruptedException {10

11 System.setProperty("webdriver.ie.driver", ".\\Tools\\IEDriverServer.exe");12 WebDriver driver = newInternetExplorerDriver();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 packagebase;2

3 importjava.util.Arrays;4 importjava.util.concurrent.TimeUnit;5

6 importorg.openqa.selenium.WebDriver;7 importorg.openqa.selenium.chrome.ChromeDriver;8 importorg.openqa.selenium.chrome.ChromeOptions;9 importorg.openqa.selenium.remote.DesiredCapabilities;10

11 public classTestChromeDriver {12

13 public static void main(String args[]) throwsInterruptedException {14

15 System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe");16 ChromeOptions options = newChromeOptions();17 DesiredCapabilities capabilities =DesiredCapabilities.chrome();18 capabilities.setCapability("chrome.switches", Arrays.asList("--start-maximized"));19 options.addArguments("--test-type", "--start-maximized");20 WebDriver driver = newChromeDriver(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 packagebase;2

3 importjava.util.concurrent.TimeUnit;4

5 importorg.openqa.selenium.WebDriver;6 importorg.openqa.selenium.firefox.FirefoxDriver;7

8 public classTestFireFoxDriver {9

10 public static void main(String args[]) throwsInterruptedException {11

12 System.setProperty("webdriver.Firefox.driver", ".\\Tools\\geckodriver.exe");13 WebDriver driver = newFirefoxDriver();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 }

浏览器驱动的下载地址

常见的错误汇总

1. org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launching Internet Explorer. Browser zoom level was set to 75%. It should be set to 100% (WARNING: The server did not provide any stacktrace information)

ie浏览器显示百分比,需要调节到100%

2. org.openqa.selenium.ElementNotVisibleException: The point at which the driver is attempting to click on the element was not scrolled into the viewport. (WARNING: The server did not provide any stacktrace information)

不同设备,窗口大小问题,页面没有显示全,需要向下滚动

3.org.openqa.selenium.SessionNotCreatedException: session not created exception: Chrome version must be >= 66.0.3359.0

Chrome浏览器驱动版本不匹配的问题,需要查询目前chrome版本,下载匹配的驱动

4.No such Element Exception

元素定位的方式有误,原因:页面源码改动

原因: 元素还不是可用的状态

原因: 出现弹窗

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值