Htmlunit填充输入时为null指针,但元素不为空 [英] Htmlunit null pointer on filling input , but element is not nulled

查看:73
本文介绍了Htmlunit填充输入时为null指针,但元素不为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在尝试通过htmlunit自动登录,这是我到目前为止的尝试,但是输出始终具有null指针异常,尽管该元素没有为空

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)

推荐答案

使用请注意,现在是HtmlEmailInput,而不是HtmlTextInput.

Please note it is now HtmlEmailInput, not HtmlTextInput.

这篇关于Htmlunit填充输入时为null指针,但元素不为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆