摘:http://www.cnblogs.com/dream0577/archive/2012/10/07/2714579.html

/**
       用IE驱动,1.先到官网下载IEDriverServer.exe,2.在代码设置属性 3.在代码设置忽略IE保护模式,4.import org.openqa.selenium.remote.DesiredCapabilities;
       */
       System.setProperty("webdriver.ie.driver", "F:\\selenium\\workspace\\fj10086\\IEDriverServer.exe");
       DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
       ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
       driver = new InternetExplorerDriver(ieCapabilities);

Selenium WebDriver使用IE浏览器

前文写到了WebDriver和JUnit的基本使用,是以Firefox为例来写的,而当我使用IE浏览器Driver来使用WebDriver时,遇到了一些问题,故记录如下。

如下的Java代码是打开IE浏览器,然后在Google中搜索“smilejay”关键字。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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”变量中,否则可能遇到报错信息:

1
2
3
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/list
2012-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”(保护模式),否则可能遇到如下的错误提示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Started InternetExplorerDriver server (64-bit)
2.25.2.0
Listening on port 40961
Exception 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 seconds
Build 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: InternetExplorerDriver
Session 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。

1
2
3
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,而没有啥好的解决方法。

1
2
3
4
5
6
7
Started InternetExplorerDriver server (64-bit)
2.25.2.0
Listening on port 44940
2012-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 respond
2012-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浏览器的更多相关文章

  1. selenium webdriver启动Chrome浏览器后无法输入网址的解决办法

    通过selenium webdriver启动Chrome浏览器,脚本如下: from selenium import webdriver browser = webdriver.Chrome() br ...

  2. selenium webdriver启动IE浏览器失败的解决办法

    通过selenium webdriver启动IE浏览器失败,报错:selenium.common.exceptions.WebDriverException: Message: Unexpected ...

  3. selenium webdriver 启动三大浏览器Firefox,Chrome,IE

    selenium webdriver 启动三大浏览器Firefox,Chrome,IE 1.安装selenium 在联网的情况下,在Windows命令行(cmd)输入pip install selen ...

  4. selenium webdriver操作各浏览器

    描述 本文主要是针对Chrome 62 , firefox57 ,和IE11 三个版本的操作.相关的driver .可点击以下链接.所有的driver 建议放在浏览器的目录下,本文中所有的driver ...

  5. Selenium WebDriver的多浏览器测试

    1. IE浏览器,需要配合下载IEDriverSever.exe的驱动程序,目前selenium支持IE9以上. (驱动程序下载链接:https://pan.baidu.com/s/1YpaUsIs1 ...

  6. Selenium webdriver 操作chrome 浏览器

    Step1: 下载chromedriver. 下载路径: http://chromedriver.storage.googleapis.com/index.html 选择一个合适的下载即可.我下载的是 ...

  7. Selenium webdriver 操作IE浏览器

    V1.0版本:直接新建WebDriver使用 import org.openqa.selenium.WebDriver; import org.openqa.selenium.ie.InternetE ...

  8. Python+Selenium WebDriver API:浏览器及元素的常用函数及变量整理总结

    由于网页自动化要操作浏览器以及浏览器页面元素,这里笔者就将浏览器及页面元素常用的函数及变量整理总结一下,以供读者在编写网页自动化测试时查阅. from selenium import webdrive ...

  9. selenium webdriver python 操作浏览器

    新建driver driver=webdriver.Firefox() driver=webdriver.Ie() driver=webdriver.Chrome()   打开一个链接 driver. ...

随机推荐

  1. springMVC下载FTP上的文件

    springMVC下载FTP上的文件 今天没时间写.先上传 一个工具类 工具类 package com.utils; import java.io.File; import java.io.FileO ...

  2. COOKIE&&SESSION

    ---------------------------------------------------------------------------COOKIE------------------- ...

  3. Hadoop MapReduce编程 API入门系列之小文件合并(二十九)

    不多说,直接上代码. Hadoop 自身提供了几种机制来解决相关的问题,包括HAR,SequeueFile和CombineFileInputFormat. Hadoop 自身提供的几种小文件合并机制 ...

  4. dubbo问题总结

    1.dubbo序列化数据丢失问题.dubbo的返回结果是对象,那么必须要实现对象中需要返回的数据的get方法. 2.dubbo序列化问题.

  5. Lambda 表达式的基本形式

    基本形式: (input parameters) => expression 其中:运算符“=>”读作“goes to”. 例如: () => 5 * 3; (x) => x ...

  6. tz2txt的安装与使用

    tz2txt是一个开源的小工具,用于把帖子的楼主发言保存为txt文件. 目前支持天涯社区.新浪论坛(大部分版块).百度贴吧. 本文介绍tz2txt的安装与使用. 本文目录: 一.下载.安装 二.使用t ...

  7. SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null' 解决办法

    当跑jndi项目时抛出:org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' fo ...

  8. Dijkstra 最短路算法(只能计算出一条最短路径,所有路径用dfs)

    上周我们介绍了神奇的只有五行的 Floyd 最短路算法,它可以方便的求得任意两点的最短路径,这称为"多源最短路".本周来来介绍指定一个点(源点)到其余各个顶点的最短路径,也叫做&q ...

  9. Swift3.0基础语法学习<五>

    异常处理: // // ViewController5.swift // SwiftBasicDemo // // Created by 思 彭 on 16/11/16. // Copyright © ...

  10. CSS3样式问题

    empty-cells 属性设置是否显示表格中的空单元格 tr:nth-child(even)偶数行的表格 li:nth-child(20)指定位置 2016-09-2813:23:45