Htmlunit空指针填充输入,但元素未被清零

时间:2017-01-24 21:43:45

标签: java htmlunit

所以我试图通过htmlunit自动登录,这是我到目前为止的试验,但输出总是有空指针异常,虽然元素没有空白

 public static void main(String[] args) throws IOException, InterruptedException {
    final WebClient webClient = new WebClient(BrowserVersion.CHROME);
    final HtmlPage page = webClient.getPage("https://sellercentral.amazon.com");
    final HtmlForm form = page.getFormByName("signIn");
    final HtmlTextInput un = form.getInputByName("email");
    final HtmlPasswordInput pass = form.getInputByName("password");
    System.out.println(un);
    System.out.println(pass);
    /*Trial 1*/
    un.select();
    un.type("testemail@gmail.com");
    System.out.println(un);

    /*Trial 2*/
    un.setValueAttribute("testemail@gmail.com");

   /*Trial 3*/
   un.setAttribute("value", "testemail@gmail.com");

这是输出(前两行证明它们没有被取消)

 HtmlTextInput[<input id="ap_email" name="email" value="" type="email" size="30" maxlength="128" tabindex="1" autocorrect="off" autocapitalize="off">]
HtmlPasswordInput[<input id="ap_password" name="password" type="password" maxlength="1024" size="20" tabindex="2" class="password">]
Exception in thread "main" java.lang.NullPointerException
    at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.hasTopCall(ScriptRuntime.java:3241)
    at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:102)
    at com.gargoylesoftware.htmlunit.javascript.host.dom.MutationObserver.attributeReplaced(MutationObserver.java:165)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireHtmlAttributeReplaced(HtmlElement.java:349)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireHtmlAttributeReplaced(HtmlElement.java:354)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireHtmlAttributeReplaced(HtmlElement.java:354)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireHtmlAttributeReplaced(HtmlElement.java:354)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireHtmlAttributeReplaced(HtmlElement.java:354)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireHtmlAttributeReplaced(HtmlElement.java:354)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireHtmlAttributeReplaced(HtmlElement.java:354)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireHtmlAttributeReplaced(HtmlElement.java:354)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.fireHtmlAttributeReplaced(HtmlElement.java:354)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.setAttributeNS(HtmlElement.java:210)
    at com.gargoylesoftware.htmlunit.html.HtmlInput.setAttributeNS(HtmlInput.java:552)
    at com.gargoylesoftware.htmlunit.html.HtmlTextInput.setAttributeNS(HtmlTextInput.java:164)
    at com.gargoylesoftware.htmlunit.html.DomElement.setAttribute(DomElement.java:467)
    at com.gargoylesoftware.htmlunit.html.HtmlTextInput.typeDone(HtmlTextInput.java:83)
    at com.gargoylesoftware.htmlunit.html.DoTypeProcessor.typeDone(DoTypeProcessor.java:178)
    at com.gargoylesoftware.htmlunit.html.DoTypeProcessor.doType(DoTypeProcessor.java:127)
    at com.gargoylesoftware.htmlunit.html.HtmlTextInput.doType(HtmlTextInput.java:63)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.type(HtmlElement.java:548)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.type(HtmlElement.java:496)
    at com.gargoylesoftware.htmlunit.html.HtmlElement.type(HtmlElement.java:481)
    at javaapplication8.JavaApplication8.main(JavaApplication8.java:39)

2 个答案:

答案 0 :(得分:2)

使用2.25-snapshot,测试用例通过。

请注意,它现在是HtmlTextInput,而不是{{1}}。

答案 1 :(得分:1)

这是HtmlUnit的已知问题。如果最新快照问题仍然存在,请问您可以验证吗? 如果是,请将您的问题报告给我们的错误跟踪器(如果您愿意,可以打开一个新问题或添加到https://sourceforge.net/p/htmlunit/bugs/1811/)。

对于设置字段,您应该使用type方法,因为键入字段不会触发attributeChange侦听器(在实际浏览器中,但HtmlUnit触发此操作,并且触发的方法在您的情况下失败)。这是我们目前正在努力解决的问题。 设置value属性或多或少是一个坏主意,因为这将触发attributeChange侦听器(就像在真实浏览器中一样)。

希望有所帮助