HtmlUnit ScriptException错误

时间:2012-10-04 06:11:15

标签: java htmlunit htmlunit-driver

我正在使用HtmlUnitDriver,&这是我的代码。

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

我得到以下异常:

Caused by: com.gargoylesoftware.htmlunit.ScriptException: Wrapped com.gargoylesoftware.htmlunit.ScriptException: SyntaxError: missing ; before statement (http://sales.liveperson.net/hcp/html/mTag.js?site=7824460#1(eval)#1)
    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.callFunction(JavaScriptEngine.java:545)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:520)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunctionIfPossible(HtmlPage.java:896)
    at com.gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeEventListeners(EventListenersContainer.java:162)
    at com.gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeBubblingListeners(EventListenersContainer.java:221)
    at com.gargoylesoftware.htmlunit.javascript.host.Node.fireEvent(Node.java:735)
    at com.gargoylesoftware.htmlunit.html.HtmlElement$2.run(HtmlElement.java:866)
    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.html.HtmlElement.fireEvent(HtmlElement.java:871)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.executeEventHandlersIfNeeded(HtmlPage.java:1162)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:202)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:440)
    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)
    ... 8 more

请帮我解决这个问题。

3 个答案:

答案 0 :(得分:6)

要求提出许多问题来引用这类问题。引发ScriptException是因为您的javascript中存在语法错误。大多数浏览器都会设法解释JS甚至出现某种错误,但HtmlUnit在这个意义上有点不灵活。

您的选择是:

  1. 纠正JS代码
  2. WebClient
  3. 中停用JS
  4. 不要使用HtmlUnit。使用具有更好JS支持的不同框架,例如PhantomJS(注意它不是基于Java的框架)

答案 1 :(得分:4)

我和liveperson mTag.js有同样的错误

问题在于压缩的mTag.js响应。

有几种解决方法:

  1. 用明确的mTag.js替换压缩的mTag.js(需要LivePerson支持)
  2. 使用'真正的'Selenium WebDriver(Firefox驱动程序或类似代码)

答案 2 :(得分:3)

_webClient.getOptions().setThrowExceptionOnScriptError(false);

适用于这种不正确的JS场景。