HtmlUnit,尝试获取表单但出现错误。

huangapple go评论117阅读模式
英文:

HtmlUnit, Trying to get the form but getting an error

问题

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class Main {

    public static void main(String[] args) {
        WebClient webClient = new WebClient();
        webClient.getOptions().setThrowExceptionOnScriptError(false);

        try {
            HtmlPage page = (HtmlPage) webClient
                    .getPage("https://www.reddit.com/login");
            HtmlForm form = page.getFormByName("AnimatedForm");
            form.getInputByName("username").setValueAttribute("myUsername");
            HtmlInput passWordInput = form.getInputByName("password");
            passWordInput.removeAttribute("disabled");
            passWordInput.setValueAttribute("myPassword");

            page = form.getInputByValue("Log In").click(); // works fine

            System.out.println(page.asText());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            webClient.close();
        }
    }

}
英文:
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;


public class Main {

    public static void main(String[] args) {
        WebClient webClient = new WebClient();
        webClient.getOptions().setThrowExceptionOnScriptError(false);

        try {
            HtmlPage page = (HtmlPage) webClient
                    .getPage("https://www.reddit.com/login");
            HtmlForm form = page.getFormByName("AnimatedForm");
            form.getInputByName("username").setValueAttribute("myUsername");
            HtmlInput passWordInput = form.getInputByName("password");
            passWordInput.removeAttribute("disabled");
            passWordInput.setValueAttribute("myPassword");

            page = form.getInputByValue("Log In").click(); // works fine

            System.out.println(page.asText());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            webClient.close();
        }
    }

}

Everytime I run this I get an error saying "com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[form] attributeName=[name] attributeValue=[AnimatedForm]." It seems to not recognize the AnimatedForm. I was just wondering why.

答案1

得分: 1

你正在使用的方法 page.getFormByName("AnimatedForm") 将会搜索带有属性 name="AnimatedForm"<form> 元素。

页面上没有名为 "AnimatedForm" 的表单,我看到一个带有 class="AnimatedForm" 的表单。如果要按类名检索元素,可以使用类似这样的方法 page.getByXPath("//div[@class='AnimatedForm']")

英文:

The method you are using page.getFormByName(&quot;AnimatedForm&quot;) will search for &lt;form&gt; having attribute name=&quot;AnimatedForm&quot;.

There is no form with the name "AnimatedForm" on the page, I see a form with class="AnimatedForm". To retrieve element by class use something like page.getByXPath(&quot;//div[@class=&#39;AnimatedForm&#39;]&quot;)

答案2

得分: 0

你会在尝试将登录功能与 Reddit 结合时遇到很大困难。这不是一个简单的过程。如果你尝试手动操作,登录会提交凭据,看起来你会异步地在浏览器中收到响应,而不会立即被重定向。实际上,用户在浏览器中被重定向到主要登陆页面之前需要大约 5 秒钟的时间。

我强烈建议你考虑利用现有的 API,而不是试图实现登录过程(我自己尝试过,每天都在使用 HTTP 协议构建网络服务……这并不简单)。

有用的资源:Reddit 的 Java 项目中可以使用的库 https://github.com/ViteFalcon/reddit4j(我没有亲自测试过,但值得一试)

Reddit API:https://www.reddit.com/dev/api/

英文:

You're really going to struggle to get the login to work with Reddit. Its not a simple process. If you try it manually, the login submits credentials and it looks like you get a response in the browser asynchronously, without being immediately redirected. In fact it takes around 5 seconds before the user is redirected to the main landing page in a browser.

I'd highly recommend you look at utilising an already available API rather than trying to implement a login process (I've tried it and work with HTTP protocol a fair bit day to day building web services... its not simple).

Useful resources: Reddit library you could use in a Java project https://github.com/ViteFalcon/reddit4j (not tested myself but worth a try)

Reddit Api: https://www.reddit.com/dev/api/

huangapple
  • 本文由 发表于 2020年10月10日 00:33:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64283978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定