selenium使用更快的Driver--HtmlUnit Driver

转自:http://sunjun041640.blog.163.com/blog/static/2562683220131024114610625/

selenium使用更快的Driver--HtmlUnit Driver  

2013-11-24 12:05:20|  分类: Java |  标签:selenium  |举报|字号 订阅

 
 
最近使用selenium时,发现driver打开浏览器挺慢的,先后使用过firefox,chrome都感觉很慢,比如淘宝首页,手工打开浏览器访问,全部渲染完成,2-3s就完成了,而使用driver来打开,一般都得要5-10s的样子,效率确实挺低的。现在可以使用HtmlUnit Driver了,它基于htmlunit,用java来模拟一个浏览器,速度很快。
官方说明如下:

HtmlUnit Driver

This is currently the fastest and most lightweight implementation of WebDriver. As the name suggests, this is based on HtmlUnit. HtmlUnit is a java based implementation of a WebBrowser without a GUI. For any language binding (other than java) the Selenium Server is required to use this driver.

这个driver是当前最快的driver实现

Usage
WebDriver driver = new HtmlUnitDriver(); //java的使用方式
 
driver = webdriver.Remote("http://localhost:4444/wd/hub", webdriver.DesiredCapabilities.HTMLUNIT) //python的使用方式
Pros
  • Fastest implementation of WebDriver//最快的
  • A pure Java solution and so it is platform independent.//纯java实现,所以是跨平台的
  • Supports JavaScript//支持js
Cons
  • Emulates other browsers’ JavaScript behaviour (see below)
JavaScript in the HtmlUnit Driver  //如何开启javascript的支持

None of the popular browsers uses the JavaScript engine used by HtmlUnit (Rhino). If you test JavaScript using HtmlUnit the results may differ significantly from those browsers.

When we say “JavaScript” we actually mean “JavaScript and the DOM”. Although the DOM is defined by the W3C each browser has its own quirks and differences in their implementation of the DOM and in how JavaScript interacts with it. HtmlUnit has an impressively complete implementation of the DOM and has good support for using JavaScript, but it is no different from any other browser: it has its own quirks and differences from both the W3C standard and the DOM implementations of the major browsers, despite its ability to mimic other browsers.

With WebDriver, we had to make a choice; do we enable HtmlUnit’s JavaScript capabilities and run the risk of teams running into problems that only manifest themselves there, or do we leave JavaScript disabled, knowing that there are more and more sites that rely on JavaScript? We took the conservative approach, and by default have disabled support when we use HtmlUnit. With each release of both WebDriver and HtmlUnit, we reassess this decision: we hope to enable JavaScript by default on the HtmlUnit at some point.

Enabling JavaScript

If you can’t wait, enabling JavaScript support is very easy:

HtmlUnitDriver driver = new HtmlUnitDriver(true); //java的调用方式
  
driver = webdriver.Remote("http://localhost:4444/wd/hub", webdriver.DesiredCapabilities.HTMLUNITWITHJS)  //python的调用方式

This will cause the HtmlUnit Driver to emulate Firefox 3.6’s JavaScript handling by default.

//上述代码会使得 HtmlUnit Driver 模拟 Firefox3.6 对 JavaScript 的处理。

 

使用HtmlUnit Driver时,需要使用selenium-server,
启动selenium-server,然后:

java

WebDriver driver =newRemoteWebDriver(new URL("http://localhost:4444/wd/hub"),
DesiredCapabilities.htmlUnitWithJs());

python

driver = webdriver.Remote("http://localhost:4444/wd/hub", webdriver.DesiredCapabilities.HTMLUNITWITHJS)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值