HTMLUnitDriver不工作+正确的maven依赖?

时间:2017-10-29 23:18:55

标签: javascript java maven selenium htmlunit-driver

我正在Java使用HTMLUnitDriver,我遇到JavaScript的问题。无论我尝试哪个网站或依赖,它根据控制台输出几乎在任何PhantomJS上崩溃。当我使用Chrome代替时,这一切都很好,并且其工作方式与FirefoxHTMLUnitDriver。另外,我不确定我应该为HTMLUnitDriver使用哪些依赖项。

以下内容应该为<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.5.3</version> <exclusions> <exclusion> <groupId>org.sourceforge.htmlunit</groupId> <artifactId>htmlunit</artifactId> </exclusion> <exclusion> <groupId>org.sourceforge.htmlunit</groupId> <artifactId>htmlunit-core</artifactId> </exclusion> <exclusion> <groupId>org.sourceforge.htmlunit</groupId> <artifactId>neko-htmlunit</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-htmlunit-driver</artifactId> <version>2.52.0</version> </dependency> 提供Selenium

HTMLUnitDriver

然而,它没有。 net.sourceforge.htmlunit:htmlunit:2.27似乎与net.sourceforge.htmlunit:htmlunit-core-js:2.27net.sourceforge.htmlunit:neko-htmlunit:2.272.27捆绑在一起,尽管有排除。

然而,

the latest version存储库表明JavaScript仍然是最新的,但它在网站上处理任何类型的HtmlUnitDriver unitDriver = new HtmlUnitDriver(); unitDriver.setJavascriptEnabled(true); 非常糟糕,因此它无法使用。

这是我开始的方式:

Caused by: com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot find function registerElement in object [object HTMLDocument]. (https://www.example.com/some-script.js#31)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:894)
    at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:637)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:518)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:774)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:750)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:102)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.loadExternalJavaScriptFile(HtmlPage.java:991)
    at com.gargoylesoftware.htmlunit.html.HtmlScript.executeScriptIfNeeded(HtmlScript.java:366)
    at com.gargoylesoftware.htmlunit.html.HtmlScript$2.execute(HtmlScript.java:247)
    at com.gargoylesoftware.htmlunit.html.HtmlScript.onAllChildrenAddedToPage(HtmlScript.java:268)
    at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:800)
    at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
    at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:756)
    at net.sourceforge.htmlunit.cyberneko.HTMLTagBalancer.callEndElement(HTMLTagBalancer.java:1236)
    at net.sourceforge.htmlunit.cyberneko.HTMLTagBalancer.endElement(HTMLTagBalancer.java:1136)
    at net.sourceforge.htmlunit.cyberneko.filters.DefaultFilter.endElement(DefaultFilter.java:226)
    at net.sourceforge.htmlunit.cyberneko.filters.NamespaceBinder.endElement(NamespaceBinder.java:345)
    at net.sourceforge.htmlunit.cyberneko.HTMLScanner$ContentScanner.scanEndElement(HTMLScanner.java:3178)
    at net.sourceforge.htmlunit.cyberneko.HTMLScanner$ContentScanner.scan(HTMLScanner.java:2141)
    at net.sourceforge.htmlunit.cyberneko.HTMLScanner.scanDocument(HTMLScanner.java:945)
    at net.sourceforge.htmlunit.cyberneko.HTMLConfiguration.parse(HTMLConfiguration.java:521)
    at net.sourceforge.htmlunit.cyberneko.HTMLConfiguration.parse(HTMLConfiguration.java:472)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.parse(HTMLParser.java:999)
    at com.gargoylesoftware.htmlunit.html.HTMLParser.parse(HTMLParser.java:250)
    at com.gargoylesoftware.htmlunit.html.HTMLParser.parseHtml(HTMLParser.java:192)
    at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(DefaultPageCreator.java:272)
    at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:160)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:522)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:396)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:313)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:668)
    ... 3 more

例外:

JavaScript

不启用JavaScript可以更好地避免异常,但网站需要HTMLUnitDriver,因此这不是解决方案。

我的依赖项有什么问题,或者PhantomJS真的只是“垃圾”吗? HTMLUnitDriver的启动时间约为5秒,如果您只是想解析一次,那么速度非常慢,这样一个像{{1}}这样更轻量级的驱动程序会派上用场......

3 个答案:

答案 0 :(得分:0)

Documentation建议从 Selenium v2.53.0起,您需要明确包含 HtmlUnitDriver 作为依赖项将其包含在 Selenium-Maven 项目中。驱动程序的版本号现在将跟踪HtmlUnit本身。

示例:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>htmlunit-driver</artifactId>
    <version>3.6.0</version>
</dependency>  

答案 1 :(得分:0)

请注意artifactId的更改,已提示here的最新版本为:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>htmlunit-driver</artifactId>
    <version>2.27</version>
</dependency>

基于Selenium 3.4.0

您可以将Selenium 3.6.0与HtmlUnitDriver 2.28-SNAPSHOT一起使用。

我建议作为一个起点,你只引用HtmlUnitDriver,它会传递所有的依赖,然后你可以使用其他所有的驱动程序。

答案 2 :(得分:0)

这样的错误
com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot find function registerElement in object [object HTMLDocument].

通常是缺少对HtmlUnit中特殊JavaScript功能的支持的结果。如果你想看到这个修复你必须

当然,欢迎单元测试和/或补丁。