您现在的位置是:主页 > news > 腾讯企业网站建设/视频营销案例

腾讯企业网站建设/视频营销案例

admin2025/5/9 9:22:32news

简介腾讯企业网站建设,视频营销案例,上海公司注册地址变更流程,南宁网站推广v1(一) Selenium2.0 VS Selenium1.0 我们已经有了Selenium1.0为什么还需要Selenium2.0呢? Selenium1.0不能处理一下事件: 1) 本机键盘和鼠标事件 2) 同源策略XSS/HTTP(S) 3) 弹出框,对话框&am…

腾讯企业网站建设,视频营销案例,上海公司注册地址变更流程,南宁网站推广v1(一) Selenium2.0 VS Selenium1.0 我们已经有了Selenium1.0为什么还需要Selenium2.0呢? Selenium1.0不能处理一下事件: 1) 本机键盘和鼠标事件 2) 同源策略XSS/HTTP(S) 3) 弹出框,对话框&am…

(一)   Selenium2.0 VS Selenium1.0

我们已经有了Selenium1.0为什么还需要Selenium2.0呢?

Selenium1.0不能处理一下事件:

1)         本机键盘和鼠标事件

2)         同源策略XSS/HTTPS

3)         弹出框,对话框(基本身份认证,自签名的证书和文件上传/下载)

Selenium2.0有简洁的APIWebDriverWebElement对象,更好的抽象。且支持多中操作系统,多语言,多浏览器。

同时Selenium2.0进行了架构的调整和升级:

Selenium2.0 = Selenium1.0 + WebDriver(也就是说Selenium2.0合并了这两个项目)

Selenium1.0可以使用任何编程语言,但是有个先决条件就是必须支持HTTP库。Selenium1.0起初就是一个Javascript库,到后面引入了SeleniumRCSeleniumRC作为一个代理服务器并且发送操作命令给Selenium Corejavascript代码,且为SeleniumRC的一部分)。SeleniumRC从测试程序接收指令并翻译,返回测试结果给测试程序。Selenium Coreclient API打开浏览器后就注入到浏览器中,然后Selenium Core接收测试程序的指令,解释成selenese命令,在浏览器执行。

Selenium1.0作为第一个基于javascript开源的web测试框架,迅速的得到了其他浏览器的支持。但是和任何其他大项目一样,Selenium1.0也不是完美的。正因为他完全是使用javascript写的,这也给他带来了致命的缺陷。为了防止恶意的javascript,所有浏览器都加强了对javascript的安全策略。这样势必导致了在一些特定场景无法使用Selenium1.0。作为一个大项目,随着时间的日积月累,Selenium1.0API变得越来也大,也越来也复杂,导致了都不知道更好的使用它改善它。

WebDriver项目是由Simon Stewart 提出的,它是一个轻便简洁的自动化测试框架。WebDriver通过尝试不同的方法去解决Selenium1.0所面临的问题。不单单是使用javascriptWebDriver会使用任何一种更合适的机制来操作浏览器。IE通过使用C++FF通过使用javascript in a XPCOM component

通过更灵活的机制去操控浏览器,那我们就能很好的绕过浏览器javascript的安全限制。当这些技术还不够用时,我们可以调用系统设备操作,尤其是当你需要一些键盘和鼠标操作时,通过这些技术,我们可以更好的模拟用户的真实浏览器操作。

当这两个框架被合并后,一个框架的缺陷被另一个框架所弥补。WebDriver对浏览器的支持需要对应框架开发工程师做对应的开发;同样Selenium必须操作真实浏览器,但是WebDriver可以HTML unit Driver来模拟浏览器,在内存中执行用例,更加的轻便。Selenium1.0解决了自动化测试中的一些常见问题,WebDriver更好的解决了沙箱限制。WebDriver不支持并行,但是Selenium Grid解决了这个问题。

 

(二)   WebDriver

a)         WebDirver小实例

WebDriver是一个用来进行复杂重复的web自动化测试的工具。意在提供一种比Selenium1.0更简单易学,有利于维护的API。它没有和任何测试框架进行绑定,所以他可以很好的在单元测试和main方法中调用。一旦创建好一个Selenium工程,你马上会发现WebDriver和其他类库一样:它是完全独立的,你可以直接使用而不需要考虑其他配置,这个Selenium RC是截然相反的。

下面我们就开始进入WebDriver的实际应用,首先大家下载好eclipse,还有从selenium官方网站下载selenium2.0的jar包和chromedriver的驱动文件。构建整个工程如下模式:

Selenium2.0之WebDriver学习总结(1) - 网易杭州QA - 网易杭州 QA Team
 现在我们开始写代码:

<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">package</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> demo</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">By</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebElement</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">firefox</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">FirefoxDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">support</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">ui</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">ExpectedCondition</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">support</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">ui</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriverWait</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">public</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">class</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">Selenium2Example</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">     </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">public</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">static</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">void</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> main</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">String</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">[]</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> args</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">)</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">//创建一个WebDriver实例</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">FirefoxDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 访问google</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">get</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"http://www.google.com"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 另一种访问方法</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// driver.navigate().to("http://www.google.com");</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 找到文本框</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebElement</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> element </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">findElement</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">By</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">name</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"q"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">));</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 输入搜索关键字</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         element</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">sendKeys</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"Selenium"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">//提交表单 WebDriver会自动从表单中查找提交按钮并提交</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         element</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">submit</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 检查页面title</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">System</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">out</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">println</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"Page title is: "</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">+</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">getTitle</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">());</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// google查询结果是通过javascript动态呈现的.</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 设置页面等待10秒超时</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriverWait</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">,</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="lit" style="line-height: 25px; color: rgb(0, 102, 102);">10</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">)).</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">until</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">ExpectedCondition</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);"><</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">Boolean</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">>()</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">              </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">public</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">Boolean</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> apply</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> d</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">)</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">                  </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">return</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> d</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">getTitle</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">().</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">toLowerCase</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">().</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">startsWith</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"Selenium"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">              </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">});</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 显示查询结果title</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">System</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">out</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">println</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"Page title is: "</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">+</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">getTitle</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">());</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">//关闭浏览器</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">quit</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">     </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span>
<span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span></p>


b)         介绍WebDriverDrivers

WebDriver是测试中必须要写的关键接口的名字,但是他有多种实现,包括下面:

HtmlUnit Driver这是目前最快,最轻巧的实施的WebDriver。顾名思义,这是基于HtmlUnit HtmlUnitJava一个WebBrowser没有图形用户界面的实现。

用法

<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span style="font-family: Arial, Helvetica, sans-serif; text-indent: 28px; white-space: normal;"><span lang="EN-US" style="color: rgb(51, 51, 51);"><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span></span></span><span lang="EN-US" style="font-family: Arial, Helvetica, sans-serif; text-indent: 28px; white-space: normal; color: rgb(51, 51, 51);"><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">driver</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span></span><span style="font-family: Arial, Helvetica, sans-serif; text-indent: 28px; white-space: normal;"><span lang="EN-US" style="color: rgb(48, 48, 48);"><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span></span></span><span lang="EN-US" style="font-family: Arial, Helvetica, sans-serif; text-indent: 28px; white-space: normal; color: rgb(51, 51, 51);"><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span></span><span style="font-family: Arial, Helvetica, sans-serif; text-indent: 28px; white-space: normal;"><span lang="EN-US" style="color: green;"><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span></span></span><span lang="EN-US" style="font-family: Arial, Helvetica, sans-serif; text-indent: 28px; white-space: normal; color: rgb(51, 51, 51);"><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">HtmlUnitDiver</span></span><span style="font-family: Arial, Helvetica, sans-serif; text-indent: 28px; white-space: normal;"><span lang="EN-US" style="color: rgb(48, 48, 48);"><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span></span></span>
</p>

例子:


<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">package</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> demo</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">By</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebElement</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">htmlunit</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">HtmlUnitDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">support</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">ui</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">ExpectedCondition</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">support</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">ui</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriverWait</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">public</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">class</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">DemoUseHtmlUnit</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">public</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">static</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">void</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> main</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">String</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">[]</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> args</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">)</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">//创建一个WebDriver实例</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">HtmlUnitDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 访问google</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">get</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"http://www.google.com"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 找到文本框</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebElement</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> element </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">findElement</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">By</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">name</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"q"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">));</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 输入搜索关键字</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         element</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">sendKeys</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"Selenium"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">//提交表单 WebDriver会自动从表单中查找提交按钮并提交</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         element</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">submit</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 检查页面title</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">System</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">out</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">println</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"Page title is: "</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">+</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">getTitle</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">());</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// google查询结果是通过javascript动态呈现的.</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 设置页面等待10秒超时</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriverWait</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">,</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="lit" style="line-height: 25px; color: rgb(0, 102, 102);">10</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">)).</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">until</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">ExpectedCondition</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);"><</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">Boolean</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">>()</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">             </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">public</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">Boolean</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> apply</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> d</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">)</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">                 </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">return</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> d</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">getTitle</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">().</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">toLowerCase</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">().</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">startsWith</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"selenium"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">             </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">});</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 显示查询结果title</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">System</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">out</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">println</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"Page title is: "</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">+</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">getTitle</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">());</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">//关闭浏览器</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">         driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">quit</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p>

优点

l  WebDriver最快的实现方式

l  java跨平台性好

l  支持javascript

缺点

l  模拟其他浏览器的JavaScript行为(见下文)

现今流行的浏览器没有使用HtmlUnit所使用的JavaScript引擎。如果使用HtmlUnit测试JavaScript的结果可能会与实际使用浏览器不同。当我们说“JAVASCRIPT”其实我们的意思是JavaScriptDOM”。虽然DOM是由W3C定义的,但是每个浏览器都有自己的方式使用JavaScript来实现DOM HtmlUnitJavaScript操作DOM具有良好的支持和完整实现,给人留下了深刻的印象,但它和任何其他浏览器一样:它有自己的方式和W3C标准的主流浏览器的DOM实现差异,尽管其有模仿其他浏览器的能力。

支持javascript

<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, sans-serif; white-space: normal;"><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">HtmlUnitDriver</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">HtmlUnitDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">true</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span></span>
</p>

注:HtmlUnitDriver默认是模拟firefox3.6来处理javascript。(具体作用还未研究)

 

Firefox Driver这是一个比较好的WebDriver,目前已经支持到了10.0版本。运行Firefox需要加载Selenium WebDriver.xpi插件。

用法:

<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span style="font-family: Arial, Helvetica, sans-serif; text-indent: 28px; white-space: normal;"><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">FirefoxDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span></span>
</p>

例子:


<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">package</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> demo</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> java</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">io</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">File</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> java</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">io</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">IOException</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">By</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebElement</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">firefox</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">FirefoxDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">firefox</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">FirefoxProfile</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">support</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">ui</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">ExpectedCondition</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">support</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">ui</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriverWait</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">public</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">class</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">DemoUseFirefox</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">public</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">static</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">void</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> main</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">String</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">[]</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> args</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">)</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">//创建一个WebDriver实例</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">FirefoxDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 访问google</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">get</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"http://www.google.com"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 找到文本框</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebElement</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> element </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">findElement</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">By</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">name</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"q"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">));</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 输入搜索关键字</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        element</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">sendKeys</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"Selenium"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">//提交表单 WebDriver会自动从表单中查找提交按钮并提交</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        element</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">submit</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 检查页面title</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">System</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">out</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">println</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"Page title is: "</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">+</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">getTitle</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">());</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// google查询结果是通过javascript动态呈现的.</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 设置页面等待10秒超时</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriverWait</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">,</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="lit" style="line-height: 25px; color: rgb(0, 102, 102);">10</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">)).</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">until</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">ExpectedCondition</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);"><</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">Boolean</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">>()</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">            </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">public</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">Boolean</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> apply</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> d</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">)</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">                </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">return</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> d</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">getTitle</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">().</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">toLowerCase</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">().</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">startsWith</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"selenium"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">            </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">});</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 显示查询结果title</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">System</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">out</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">println</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"Page title is: "</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">+</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">getTitle</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">());</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">//关闭浏览器</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">quit</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span>
<span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span></p>

优点:

l  在真正的浏览器上运行,且支持javascript

l  运行速度快于IE

缺点:

l  运行速度低于HtmlUnit Driver

修改Firefox的配置文件:FirefoxDriver可以通过自己设置Firefox的配置文件,如启动浏览器时,加载Firebug插件。

<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">File</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> file </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">File</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">".\\res\\firebug-1.9.1-fx.xpi"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">FirefoxProfile</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> firefoxProfile </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">FirefoxProfile</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">try</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  firefoxProfile</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">addExtension</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">file</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">catch</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">IOException</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> e</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">)</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// TODO Auto-generated catch block</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  e</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">printStackTrace</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> firefoxProfile</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">setPreference</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"extensions.firebug.currentVersion"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">,</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"1.9.1"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span></p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p>


 例子:

<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;">
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">package</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> demo</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> java</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">io</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">File</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> java</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">io</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">IOException</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">By</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebElement</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">firefox</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">FirefoxDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">firefox</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">FirefoxProfile</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">support</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">ui</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">ExpectedCondition</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">support</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">ui</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriverWait</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">public</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">class</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">DemoUseFirefox</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">public</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">static</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">void</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> main</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">String</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">[]</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> args</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">)</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 创建一个WebDriver实例</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">File</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> file </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">File</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">".\\res\\firebug-1.9.1-fx.xpi"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// firebug插件的本地位置</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">FirefoxProfile</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> firefoxProfile </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">FirefoxProfile</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">try</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">   firefoxProfile</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">addExtension</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">file</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">catch</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">IOException</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> e</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">)</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">   </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// TODO Auto-generated catch block</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">   e</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">printStackTrace</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  firefoxProfile</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">setPreference</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"extensions.firebug.currentVersion"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">,</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"1.9.1"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">FirefoxDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">firefoxProfile</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 访问google</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">get</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"http://www.google.com"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 另一种访问方法</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebElement</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> element </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">findElement</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">By</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">name</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"q"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">));</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 输入搜索关键字</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  element</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">sendKeys</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"Selenium"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 提交表单 WebDriver会自动从表单中查找提交按钮并提交</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  element</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">submit</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 检查页面title</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">System</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">out</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">println</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"Page title is: "</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">+</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">getTitle</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">());</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// google查询结果是通过javascript动态呈现的.</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 设置页面等待10秒超时</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriverWait</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">,</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="lit" style="line-height: 25px; color: rgb(0, 102, 102);">10</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">)).</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">until</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">ExpectedCondition</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);"><</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">Boolean</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">>()</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">   </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">public</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">Boolean</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> apply</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> d</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">)</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">    </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">return</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> d</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">getTitle</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">().</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">toLowerCase</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">().</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">startsWith</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"selenium"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">   </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">});</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 显示查询结果title</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">System</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">out</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">println</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"Page title is: "</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">+</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">getTitle</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">());</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 关闭浏览器</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">quit</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span>
<span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span>
</p><div>
</div>


Internet Explorer Driver该驱动需要一个dll文件,故只能在windows系统下使用。所有Selenium版本的都有对xp下的IE6,7,8windows7下的IE9支持。

用法:

<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span style="font-family: Arial, Helvetica, sans-serif; text-indent: 28px; white-space: normal;"><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">InternetExplorerDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span></span></p>

例子:

<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;">
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">package</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> demo</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">By</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebElement</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">ie</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">InternetExplorerDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">support</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">ui</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">ExpectedCondition</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">support</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">ui</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriverWait</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">public</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">class</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">DemoUseIE</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">public</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">static</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">void</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> main</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">String</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">[]</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> args</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">)</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 创建一个WebDriver实例</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">InternetExplorerDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 访问google</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">get</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"http://www.google.com"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 找到文本框</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebElement</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> element </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">findElement</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">By</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">name</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"q"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">));</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 输入搜索关键字</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  element</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">sendKeys</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"Selenium"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 提交表单 WebDriver会自动从表单中查找提交按钮并提交</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  element</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">submit</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 检查页面title</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">System</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">out</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">println</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"Page title is: "</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">+</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">getTitle</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">());</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// google查询结果是通过javascript动态呈现的.</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 设置页面等待10秒超时</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriverWait</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">,</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="lit" style="line-height: 25px; color: rgb(0, 102, 102);">10</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">)).</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">until</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">ExpectedCondition</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);"><</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">Boolean</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">>()</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">   </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">public</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">Boolean</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> apply</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> d</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">)</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">    </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">return</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> d</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">getTitle</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">().</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">toLowerCase</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">().</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">startsWith</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"selenium"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">   </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">});</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 显示查询结果title</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">System</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">out</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">println</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"Page title is: "</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">+</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">getTitle</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">());</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 关闭浏览器</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">quit</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span>
<span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span></p>

优点:

l  运行于真正的浏览器,且支持各种用户终端的javascript

缺点:

l  IE Driver只能工作在windows

l  运行速度相对较慢

l  大部分版本下XPach没有原生支持,由于Sizzle是自动注入,所以运行速度低于其他浏览器,CSS渲染也比较慢;

l  CSS不是原生支持IE6IE7的,Sizzle是强行注入的;

l  IE8,9是原生支持CSS的,但是他们不完全支持CSS3

需要的配置项:

l  ie7还有win7vista系统下,我们必须将浏览器设置成安全模式Selenium2.0之WebDriver学习总结 - 网易杭州QA - 网易杭州 QA Team

 

l  如果要使用鼠标事件,必须确定浏览器是100%缩放的

 

Chrome Driver一个独立的服务器,是由Chromium项目组协助开发的。ChromeDriver由三个部件组成:chrome浏览器本身,支持ChromeDriver的语言和支持ChromeDriverChrome之间通信的工程。使用ChromeDriver必须启动一个后台程序(即:支持ChromeDriver的语言和支持ChromeDriverChrome之间通信的工程)。

用法:

<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span style="font-family: Arial, Helvetica, sans-serif; white-space: normal;"><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">ChromeDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span></span></p>

例子:

<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">package</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> demo</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">By</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebElement</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">chrome</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">ChromeDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">support</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">ui</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">ExpectedCondition</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">support</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">ui</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriverWait</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">public</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">class</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">DemoUseChrome</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">public</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">static</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">void</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> main</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">String</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">[]</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> args</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">)</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 配置服务器</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">System</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">setProperty</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"webdriver.chrome.driver"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">,</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">    </span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">".\\res\\chromedriver.exe"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 创建一个WebDriver实例</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">ChromeDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 访问google</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">get</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"http://www.google.com"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 找到文本框</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebElement</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> element </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">findElement</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">By</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">name</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"q"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">));</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 输入搜索关键字</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  element</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">sendKeys</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"Selenium"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 提交表单 WebDriver会自动从表单中查找提交按钮并提交</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  element</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">submit</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 检查页面title</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">System</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">out</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">println</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"Page title is: "</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">+</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">getTitle</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">());</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// google查询结果是通过javascript动态呈现的.</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 设置页面等待10秒超时</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriverWait</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">,</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="lit" style="line-height: 25px; color: rgb(0, 102, 102);">10</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">)).</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">until</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">ExpectedCondition</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);"><</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">Boolean</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">>()</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">   </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">public</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">Boolean</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> apply</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> d</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">)</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">    </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">return</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> d</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">getTitle</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">().</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">toLowerCase</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">().</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">startsWith</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"selenium"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">   </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">});</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 显示查询结果title</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">System</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">out</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">println</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"Page title is: "</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">+</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">getTitle</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">());</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 关闭浏览器</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">quit</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span>
<span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span></p>


优点:

l  运行在真正的浏览器上,且支持javascript

l  由于ChromeWebkit内核的浏览器,所以Chrome Driver可以一定程度上测试Safari。但是需要注意的是Chrome使用自己的V8 javascript引擎,而SafariNitro引擎,在javascript的执行上还是有区别的。

缺点:

l  运行速度低于HtmlUnit Driver

Chrome高级设置:

我们可以使用一些命令行指令来配置浏览器,类似Firefox。如最大化浏览器:

<p style="margin-top: 10px; margin-bottom: 10px; margin-left: 21pt; padding-top: 0px; padding-bottom: 0px; font-family: Arial, Helvetica, sans-serif; white-space: normal;"><span lang="EN-US"><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">DesiredCapabilities</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> capabilities </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">DesiredCapabilities</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">chrome</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; margin-left: 21pt; padding-top: 0px; padding-bottom: 0px; font-family: Arial, Helvetica, sans-serif; white-space: normal;"><span lang="EN-US"><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  capabilities</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">setCapability</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"chrome.switches"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">,</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">Arrays</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">asList</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"--start-maximized"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">));</span></span></p><p style="margin-top: 10px; margin-bottom: 10px; margin-left: 21pt; padding-top: 0px; padding-bottom: 0px; font-family: Arial, Helvetica, sans-serif; white-space: normal;"><span lang="EN-US"><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">ChromeDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">capabilities</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span></span></p>

例子:

<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">package</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> demo</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> java</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">util</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">Arrays</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">By</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebElement</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">chrome</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">ChromeDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">remote</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">DesiredCapabilities</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">support</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">ui</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">ExpectedCondition</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">support</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">ui</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriverWait</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">public</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">class</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">DemoUseChrome</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">public</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">static</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">void</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> main</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">String</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">[]</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> args</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">)</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 配置服务器</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">System</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">setProperty</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"webdriver.chrome.driver"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">,</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">    </span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">".\\res\\chromedriver.exe"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 创建一个WebDriver实例</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">DesiredCapabilities</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> capabilities </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">DesiredCapabilities</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">chrome</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  capabilities</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">setCapability</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"chrome.switches"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">,</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">    </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">Arrays</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">asList</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"--start-maximized"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">));</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">ChromeDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">capabilities</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 访问google</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">get</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"http://www.google.com"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 另一种访问方法</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// driver.navigate().to("http://www.google.com");</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 找到文本框</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebElement</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> element </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">findElement</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">By</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">name</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"q"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">));</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 输入搜索关键字</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  element</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">sendKeys</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"Selenium"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 提交表单 WebDriver会自动从表单中查找提交按钮并提交</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  element</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">submit</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 检查页面title</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">System</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">out</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">println</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"Page title is: "</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">+</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">getTitle</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">());</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// google查询结果是通过javascript动态呈现的.</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 设置页面等待10秒超时</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriverWait</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">,</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="lit" style="line-height: 25px; color: rgb(0, 102, 102);">10</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">)).</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">until</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">ExpectedCondition</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);"><</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">Boolean</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">>()</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">   </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">public</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">Boolean</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> apply</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> d</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">)</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">    </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">return</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> d</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">getTitle</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">().</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">toLowerCase</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">().</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">startsWith</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"selenium"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">   </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">});</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 显示查询结果title</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">System</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">out</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">println</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"Page title is: "</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">+</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">getTitle</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">());</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 关闭浏览器</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">  driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">quit</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span>
<span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span>
</p><div>
</div>


WebDriver-Backed Selenium-RC:WebDriver提供了Selenium-RC的实现,这表示你可以通过Selenium-RC API来使用WebDriver的基础功能。这主要是为了向后兼容。他允许通过适当的转换,那些已经用Selenium-RC API编写的用例能在WebDriver下使用。

用法:

<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">Selenium</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> selenium </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriverBackedSelenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">,</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> baseUrl</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span></p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p>

例子:

<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">package</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> demo</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriverBackedSelenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">firefox</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">FirefoxDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">support</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">ui</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">ExpectedCondition</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> org</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">openqa</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">support</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">ui</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriverWait</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">import</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> com</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">thoughtworks</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">Selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">public</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">class</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">DemoUseBacked</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">public</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">static</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">void</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> main</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">String</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">[]</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> args</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">)</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">//创建一个WebDriver实例</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">FirefoxDriver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">String</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> baseUrl </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"http://www.youdao.com"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">;</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">//启动一个Selenium</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">Selenium</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> selenium </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriverBackedSelenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">,</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> baseUrl</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 访问google</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">open</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">baseUrl</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 输入搜索关键字</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">type</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"id=query"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">,</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"selenium"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">//点击 搜索</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">click</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"id=qb"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 检查页面title</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">System</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">out</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">println</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"Page title is: "</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">+</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">getTitle</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">());</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// google查询结果是通过javascript动态呈现的.</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 设置页面等待10秒超时</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriverWait</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">,</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="lit" style="line-height: 25px; color: rgb(0, 102, 102);">10</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">)).</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">until</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">ExpectedCondition</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);"><</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">Boolean</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">>()</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">            </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">public</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">Boolean</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> apply</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">WebDriver</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> d</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">)</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">{</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">                </span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">return</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> d</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">getTitle</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">().</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">toLowerCase</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">().</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">startsWith</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"selenium"</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">);</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">            </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">});</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">// 显示查询结果title</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="typ" style="line-height: 25px; color: rgb(102, 0, 102);">System</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="kwd" style="line-height: 25px; color: rgb(0, 0, 136);">out</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">println</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">(</span><span class="str" style="line-height: 25px; color: rgb(0, 136, 0);">"Page title is: "</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">+</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);"> driver</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">getTitle</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">());</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        </span><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">//停止Selenium</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">        selenium</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">stop</span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">();</span>
<span class="pln" style="line-height: 25px; color: rgb(0, 0, 0);">    </span><span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span>
<span class="pun" style="line-height: 25px; color: rgb(102, 102, 0);">}</span>
</p><div>
</div>


优点:

l  允许WebDriverSelenium API直接兼容

l  提供selenium RCWebDriver迁移

l  不需要对立的运行Selenium RC server

缺点:

 l  不是所有方法都可以使用

l  由于底层实现的差别,切换后有些方法会运行变慢