【问题标题】:ReferenceError: "validate" is not defined. while trying to submit a form with HTMLUnitReferenceError:未定义“验证”。在尝试使用 HTMLUnit 提交表单时
【发布时间】: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


【解决方案1】:

在顶部添加这一行:

webClient.getOptions().setJavaScriptEnabled(false);

【讨论】:

    【解决方案2】:

    Edge 支持还不完善,请您尝试使用 Firefox 52 或 Chrome 模拟。另外请使用最新的 Snapshot 版本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-06
      • 2015-11-08
      • 2019-09-25
      • 1970-01-01
      • 2012-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多