Search code examples
javahtmlunit

ClassCastException when trying to use getPage in HTMLUnit


Here is the simple code snippet I am using:

final WebClient client = new WebClient();
final HtmlPage page = client.getPage("http://www.muthead.com/packs/simulator/12-tokearmy-pack");

This is giving me the following:

Exception in thread "main" ======= EXCEPTION START ========
Exception class=[java.lang.ClassCastException]
com.gargoylesoftware.htmlunit.ScriptException: com.gargoylesoftware.htmlunit.javascript.host.html.HTMLAnchorElement cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlElement
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:921)
    at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:628)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:515)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:852)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:824)
    at com.gargoylesoftware.htmlunit.InteractivePage.executeJavaScriptFunctionIfPossible(InteractivePage.java:216)
    at com.gargoylesoftware.htmlunit.javascript.host.event.EventListenersContainer.executeEventListeners(EventListenersContainer.java:258)
    at com.gargoylesoftware.htmlunit.javascript.host.event.EventListenersContainer.executeBubblingListeners(EventListenersContainer.java:322)
    at com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget.fireEvent(EventTarget.java:206)
    at com.gargoylesoftware.htmlunit.html.DomElement$2.run(DomElement.java:1190)
    at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:628)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:515)
    at com.gargoylesoftware.htmlunit.html.DomElement.fireEvent(DomElement.java:1195)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.executeEventHandlersIfNeeded(HtmlPage.java:1198)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:238)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:519)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:386)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:304)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:451)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:436)
    at Runner.main(Runner.java:26)
== CALLING JAVASCRIPT ==
  function (l) {
      k.call(i, l);
  }
======= EXCEPTION END ========

The weird thing is that if I switch out the link for http://google.com, everything works fine. Does this mean there is a problem with the web page I am trying to link to? (Even though it is a valid link)


Solution

  • This has been recently fixed int 2.27-SNAPSHOT, and your test case passes now with latest build (login as 'guest').

    Please update accordingly.