首页 > 代码库 > Selenium WebDriver使用IE浏览器(转)
Selenium WebDriver使用IE浏览器(转)
转自:http://smilejay.com/2012/09/selenium-webdriver-with-ie/
如下的Java代码是打开IE浏览器,然后在Google中搜索“smilejay”关键字。
1234567891011121314151617181920212223242526272829303132333435363738 | package com.selenium.test; import org.openqa.selenium.ie.InternetExplorerDriver;import org.openqa.selenium.remote.DesiredCapabilities;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement; public class TempGoogle { public static void main(String[] args) { final String sUrl = "http://www.google.com.hk/"; System.setProperty("webdriver.ie.driver","C:\\Users\\yren9\\workspace\\selenium\\IEDriverServer.exe"); DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer(); ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true); WebDriver oWebDriver = new InternetExplorerDriver(ieCapabilities); oWebDriver.get(sUrl); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // Use name locator to identify the search input field. WebElement oSearchInputElem = oWebDriver.findElement(By.name("q")); oSearchInputElem.sendKeys("smilejay"); WebElement oGoogleSearchBtn = oWebDriver.findElement(By.xpath("//input[@name=‘btnK‘]")); oGoogleSearchBtn.click(); try { Thread.sleep(5000); } catch(InterruptedException ex) { System.out.println(ex.getMessage()); } oWebDriver.close(); }} |
上面的代运行行是没有错误的,不过,类似的程序,如果没有写的很好,或IE浏览器环境没设置好,在Eclipse中可能会遇到如下的一些错误提示。
1. 需要设置IE的Driver到“webdriver.ie.driver”变量中,否则可能遇到报错信息:
123 | The path to the driver executable must be set by the webdriver.ie.driver system property; for more information, see http://code.google.com/p/selenium/wiki/InternetExplorerDriver. The latest version can be downloaded from http://code.google.com/p/selenium/downloads/list2012-9-2 16:09:02 org.openqa.selenium.ie.InternetExplorerDriverServer initializeLib警告: This method of starting the IE driver is deprecated and will be removed in selenium 2.26. Please download the IEDriverServer.exe from http://code.google.com/p/selenium/downloads/list and ensure that it is in your PATH. |
更具提示,需要添加IEDriverServer.exe(从Selenium官网可下载的),并用如下的代码进行设置。
System.setProperty(“webdriver.ie.driver”,”C:\\Users\\yren9\\workspace\\selenium\\IEDriverServer.exe”);
2. 如果IE浏览器设置安全性较高,在“Internet Options”中都不要选择“Enable Protected Mode”(保护模式),否则可能遇到如下的错误提示。
12345678910111213141516171819202122 | Started InternetExplorerDriver server (64-bit)2.25.2.0Listening on port 40961Exception in thread "main" org.openqa.selenium.WebDriverException: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones. (WARNING: The server did not provide any stacktrace information)Command duration or timeout: 1.18 secondsBuild info: version: ‘2.25.0‘, revision: ‘17482‘, time: ‘2012-07-18 22:18:01‘System info: os.name: ‘Windows 7‘, os.arch: ‘x86‘, os.version: ‘6.1‘, java.version: ‘1.6.0_29‘Driver info: driver.version: InternetExplorerDriverSession ID: 01e30b64-e403-440c-bed8-4859ef2128f9 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:188) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:498) at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:182) at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:167) at org.openqa.selenium.ie.InternetExplorerDriver.startSession(InternetExplorerDriver.java:133) at org.openqa.selenium.ie.InternetExplorerDriver.setup(InternetExplorerDriver.java:106) at org.openqa.selenium.ie.InternetExplorerDriver.<init>(InternetExplorerDriver.java:52) at com.selenium.test.TempGoogle.main(TempGoogle.java:15) |
解决方法有两种,一种是修改掉IE的设置,不要在任何情况下使用保护模式(protected mode),另一种即是前面代码中如下片段在运行时设置IE的Capabilities。
123 | DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);WebDriver oWebDriver = new InternetExplorerDriver(ieCapabilities); |
3. 即便是我上面已经修改好的代码(解决了1、2两个问题),在Eclipse中也会有如下的一些运行时警告(我的Win7上的IE9有这个问题),社区上有人也反映了这个问题,但是对测试程序运行时的功能没有影响,作为一个warning,而没有啥好的解决方法。
1234567 | Started InternetExplorerDriver server (64-bit)2.25.2.0Listening on port 449402012-9-2 16:58:19 org.apache.http.impl.client.DefaultRequestDirector tryExecute信息: I/O exception (org.apache.http.NoHttpResponseException) caught when processing request: The target server failed to respond2012-9-2 16:58:19 org.apache.http.impl.client.DefaultRequestDirector tryExecute信息: Retrying request |
关于这个的一个讨论话题: https://code.google.com/p/selenium/issues/detail?id=2568
Selenium WebDriver使用IE浏览器(转)