我想使用HTMLUnit將表單提交到this網站,但我在標題中看到錯誤。ReferenceError:「validate」未定義。同時嘗試提交HTMLUnit的格式
這裏是我的代碼:
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">
如何我能得到這個固定的?
你能後的代碼稍大的部分?你確定這個例外與你在這裏發佈的代碼有關嗎? – Assafs
我確定。其餘的代碼是不相關的。 –
在哪裏定義了textField和url? – Assafs