【发布时间】:2018-02-01 05:44:00
【问题描述】:
我想使用 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">
我怎样才能解决这个问题?
【问题讨论】:
-
你能发布一个稍微大一点的代码吗?您确定异常与您在此处发布的代码有关吗?
-
我确定。其余代码无关紧要。
-
textField 和 url 定义在哪里?
-
文本字段在我提供的代码的第 7 行,这几乎是 url 的定义(我无法提供确切的 url,因为其中有一些敏感信息,但它的 smth 像这样当我将它放在浏览器中时,它会尝试下载文件并且文件是正确的:“api.telegram.org/file/aaaa/music/LARGHETTO.mp3”这实际上是出现错误的文件之一。
标签: javascript java web-crawler htmlunit referenceerror