ReferenceError:未定义“validate”。尝试使用HTMLUnit

时间:2017-08-23 14:49:35

标签: javascript java web-crawler htmlunit referenceerror

我想使用HTMLUnit向this网站提交表单,但我在标题中收到错误。

这是我的代码:

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());
}

以下是我得到的错误:

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....

我试图关闭脚本错误,但它无效。

顺便说一下,我在网站的HTML中找到了这个:

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

我怎样才能解决这个问题?

2 个答案:

答案 0 :(得分:1)

在顶部添加此行:

webClient.getOptions().setJavaScriptEnabled(false);

答案 1 :(得分:0)

Edge支持仍然不完整,您可以尝试使用Firefox 52或Chrome模拟。另请使用最新的Snapshot版本。