这个问题是对这个问题的补充: How to log into Facebook programmatically using Java?
我已经使用(稍微修改过的版本)以下代码登录其他网站的帐户,效果很好。
WebClient webClient = new WebClient();
HtmlPage page1 = webClient.getPage("http://www.facebook.com");
HtmlForm form = page1.getForms().get(0);
HtmlSubmitInput button = (HtmlSubmitInput) form.getInputsByValue("Login").get(0);
HtmlTextInput textField = form.getInputByName("email");
textField.setValueAttribute("bob@smith.com");
HtmlPasswordInput textField2 = form.getInputByName("pass");
textField2.setValueAttribute("ladeeda");
HtmlPage page2 = button.click();
但是,每当我尝试使用正确的电子邮件和密码登录 facebook 时,我都会遇到两个问题:
SEVERE: Job run failed with unexpected RuntimeException: TypeError: Cannot find
function addImport in object [object].
(http://static.ak.fbcdn.net/rsrc.php/yC/r/gmR3y_ARtaM.js#10)
Exception class=[com.gargoylesoftware.htmlunit.ScriptException]
com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot read property "length"
from undefined (http://static.ak.fbcdn.net/rsrc.php/yC/r/gmR3y_ARtaM.js#10)
我做错了什么?
最佳答案
看起来 htmlunit 不喜欢某些 javascript。
尝试将其关闭,因为它不是登录所必需的:
webClient.setJavaScriptEnabled(false);
关于java - 使用 HTMLUnit 以编程方式使用 Java 登录 Facebook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4757233/