2

我正在使用 HtmlUnitDriver 打开一个链接。这是我的代码:

HtmlUnitDriver webDriver = new HtmlUnitDriver();
webDriver.get("some url here");

但我得到以下异常。

Caused by: com.gargoylesoftware.htmlunit.ScriptException: Wrapped com.gargoylesoftware.htmlunit.ScriptException: Wrapped com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot find function aa in object [object Object]. (https://apis.google.com/_/apps-static/_/js/gapi/plusone/rt=j/ver=use0LfpKko4.en_US./sv=1/am=!CVDWKorrwvOYBs7wcQ/d=1/rs=AItRSTMxl0wiQySaT0IYW5gyOkdlVL9A6A/cb=gapi.loaded_0#3)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:595)
    at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:537)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:538)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:499)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.loadExternalJavaScriptFile(HtmlPage.java:973)
    at com.gargoylesoftware.htmlunit.html.HtmlScript.executeScriptIfNeeded(HtmlScript.java:349)
    at com.gargoylesoftware.htmlunit.html.HtmlScript$1.execute(HtmlScript.java:230)
    at com.gargoylesoftware.htmlunit.html.HtmlScript.onAllChildrenAddedToPage(HtmlScript.java:240)
    at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:598)
    at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
    at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:556)
    at org.cyberneko.html.HTMLTagBalancer.callEndElement(HTMLTagBalancer.java:1142)
    at org.cyberneko.html.HTMLTagBalancer.endElement(HTMLTagBalancer.java:1044)
    at org.cyberneko.html.filters.DefaultFilter.endElement(DefaultFilter.java:206)
    at org.cyberneko.html.filters.NamespaceBinder.endElement(NamespaceBinder.java:329)
    at org.cyberneko.html.HTMLScanner$ContentScanner.scanEndElement(HTMLScanner.java:3018)
    at org.cyberneko.html.HTMLScanner$ContentScanner.scan(HTMLScanner.java:2005)
    at org.cyberneko.html.HTMLScanner.scanDocument(HTMLScanner.java:908)
    at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:499)
    at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:452)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.parse(HTMLParser.java:789)
    at com.gargoylesoftware.htmlunit.html.HTMLParser.parse(HTMLParser.java:225)
    at com.gargoylesoftware.htmlunit.html.HTMLParser.parseHtml(HTMLParser.java:179)
    at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(DefaultPageCreator.java:221)
    at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:106)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:433)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:311)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:373)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:346)
    ... 15 more

请指导我,如何解决这个问题。我正在使用 selenium-java-2.24.1.jar,webdriver-common.jar。

4

3 回答 3

5

异常表示驱动程序在加载页面时遇到 Javascript 函数问题,默认情况下 HTMLUNITDriver 禁用 Javascript。

您需要明确启用它,然后尝试导航 URL 试试这个,它应该可以工作。

HtmlUnitDriver driver = new HtmlUnitDriver(true);

True --> 这里提到启用 Java 脚本。

此外,HtmlUnitDriver 使用的 javascript 引擎与其他浏览器不同。查看此链接了解更多详细信息。

http://seleniumhq.org/docs/03_webdriver.html#htmlunit-driver

于 2012-10-03T09:17:42.667 回答
3

驱动程序 = 新 HtmlUnitDriver(true);

再添加一行 -

java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);

于 2012-10-23T06:19:07.887 回答
2

你可能想试试这个。

它对我来说很好。这是您可能希望对 Java 进行细微更改的 Scala 代码。

val  driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_3_6)
driver.setJavascriptEnabled(true)
driver.get(your_url)
driver.getTitle() must contain("Welcome")
于 2013-10-18T08:33:21.283 回答