selenium使用更快的Driver--HtmlUnit Driver

来源:互联网 发布:英智brecruit知乎 编辑:程序博客网 时间:2024/06/16 20:41
        最近使用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 = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), DesiredCapabilities.htmlUnitWithJs());

python:

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

http://docs.seleniumhq.org/docs/03_webdriver.jsp#htmlunit-driver
0 0
原创粉丝点击