HtmlUnit ScriptException errors

I am using HtmlUnitDriver,& here is my code.

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

I am getting following Exception:

Caused by: com.gargoylesoftware.htmlunit.ScriptException: Wrapped com.gargoylesoftware.htmlunit.ScriptException: SyntaxError: missing ; before statement (
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(
    at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunctionIfPossible(
    at com.gargoylesoftware.htmlunit.html.HtmlElement$
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireEvent(
    at com.gargoylesoftware.htmlunit.html.HtmlPage.executeEventHandlersIfNeeded(
    at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(
    at com.gargoylesoftware.htmlunit.WebClient.getPage(
    at com.gargoylesoftware.htmlunit.WebClient.getPage(
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(
    ... 8 more

Please help me to sort out this issue.


Many questions are asked referencing this kind of issues. The ScriptException is raised because you have a syntactical error in your javascript. Most browsers manage to interpret the JS even with some kind of errors but HtmlUnit is a bit inflexible in that sense.

Your options are:

  1. Correct JS code
  2. Disable JS in the WebClient
  3. Don't use HtmlUnit. Use a different framework with better JS support such as PhantomJS (note it is not a Java-based framework)

I had the same error with the liveperson mTag.js

The problem is with the compacted mTag.js response.

There are several workaround:

  1. Replace the compacted mTag.js with a clear one (requires LivePerson support)
  2. Work with a 'real' Selenium WebDriver (Firefox Driver or similar)


works in this incorrect JS scenario.

Need Your Help