Search code examples
javascriptjavaweb-crawlerhtmlunitreferenceerror

ReferenceError: "validate" is not defined. while trying to submit a form with HTMLUnit


I want to submit a form to this site with HTMLUnit but I get the error in title.

Here is my code:

try (final WebClient webClient = new WebClient(BrowserVersion.CHROME, "127.0.0.1", 57703)) {
    //webClient.getOptions().setThrowExceptionOnScriptError(false);
    final HtmlPage page1 = webClient.getPage("http://tagmp3.net/");
    final HtmlDivision div = (HtmlDivision) page1.getElementById("urlForm");
    final DomNodeList<DomElement> forms = page1.getElementsByTagName("form");
    final HtmlForm form = (HtmlForm) forms.get(0);
    final HtmlTextInput textField = form.getInputByName("filepath");
    final HtmlSubmitInput button = form.getInputByName("button");
    textField.setValueAttribute(url);
    final HtmlPage page2 = button.click();
    System.out.println(page2.getBaseURL());
}

And here is the Error I get:

EcmaError: lineNumber=[4] column=[0] lineSource=[<no source>] name=[TypeError] sourceName=[http://code.jquery.com/jquery-3.1.1.min.js] message=[TypeError: Cannot set property "innerHTML" of undefined to "<form></form><form></form>" (http://code.jquery.com/jquery-3.1.1.min.js#4)]
com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot set property "innerHTML" of undefined to "<form></form><form></form>" (http://code.jquery.com/jquery-3.1.1.min.js#4)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:894)
    at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:637)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:518)      
    blah blah blah....

I tried to turn off script error but it didn't work.

By the way I found this in the HTML of the website:

<form onsubmit="return validate(this)" method="post" action="index.php">

How can I get this fixed?


Solution

  • Add this line on top:

    webClient.getOptions().setJavaScriptEnabled(false);