英文:
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("AnimatedForm")
will search for <form>
having attribute name="AnimatedForm"
.
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("//div[@class='AnimatedForm']")
答案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/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论