Htmlunit null pointer on filling input , but element is not nulled

前提是你 提交于 2019-12-24 09:05:09

问题


So i am trying to automate a login through htmlunit and here are my trials so far , however the output always have null pointer exception , although the element is not nulled

 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");

Here is the output ( first 2 lines proof that they are not nulled )

 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)

回答1:


With 2.25-snapshot, the test case passes.

Please note it is now HtmlEmailInput, not HtmlTextInput.




回答2:


This is a known issue with HtmlUnit. Can you please verify, if the problem still exists with the latest snapshot. If yes, please report your problem to our bug tracker (if you like you can open a new one or add to https://sourceforge.net/p/htmlunit/bugs/1811/).

For setting the field you should use the type method because typing into a field does not trigger the attributeChange listener (in real browsers but HtmlUnit triggers this and the triggered method fails in your case). This is what we try to fix at the moment. Setting the value attribute is more or less a bad idea because this will trigger the attributeChange listener (like in real browsers).

Hope that helps



来源:https://stackoverflow.com/questions/41839518/htmlunit-null-pointer-on-filling-input-but-element-is-not-nulled

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!