面对HtmlUnitDriver的问题

时间:2012-10-03 08:04:23

标签: java selenium htmlunit

我正在使用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。

3 个答案:

答案 0 :(得分:5)

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

您需要明确启用它,然后尝试导航URL 试试这个它应该有效。

HtmlUnitDriver driver = new HtmlUnitDriver(true);

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

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

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

答案 1 :(得分:3)

  

driver = new HtmlUnitDriver(true);

再添加一行 -

  

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

答案 2 :(得分:2)

您可能想尝试一下。

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

val  driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_3_6)
driver.setJavascriptEnabled(true)
driver.get(your_url)
driver.getTitle() must contain("Welcome")