2012-10-03 111 views
2

我使用HtmlUnitDriver打開鏈接。 這裏是我的代碼:與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 

請指引我,如何解決this.I正在使用硒Java的2.24.1.jar,webdriver的-common.jar。

+3

「HTMLUnit」在執行JavaScript函數時遇到困難。除非你能提供更多的背景知識,否則診斷問題相當困難。 –

回答

5

異常說,加載頁面時,驅動程序面臨Javascript功能問題,默認情況下,HTMLUNITDriver禁用Javascript。

您需要明確啓用它,然後嘗試導航URL 試試這個,它應該工作。

HtmlUnitDriver driver = new HtmlUnitDriver(true); 

True - >這裏提到啓用Java腳本。

此外,HtmlUnitDriver使用的JavaScript引擎與其他瀏覽器不同。請查看此鏈接瞭解更多詳細信息。

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

+0

感謝Anuragh27crony。 – dhananjay

3

驅動=新HtmlUnitDriver(真);

再添加一個線 -

java.util.logging.Logger.getLogger( 「com.gargoylesoftware」)setLevel(Level.OFF)。

2

您可能想試試這個。

它對我來說工作正常。這是您可能希望對Java進行微小更改的Scala代碼。

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