Using HTMLUnit to log into Facebook programmatically using Java

南笙酒味 提交于 2019-12-06 06:36:27

问题


This question is kinda an addition to this question: How to log into Facebook programmatically using Java?

I have used (a slightly modified version of) the following code to log into accounts of other websites just fine.

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

However, whenever I attempt to log into facebook with correct email and password, I run into two problems:

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)

What am I doing wrong?


回答1:


It looks like htmlunit doesn't like some of the javascript.

Try switching it off as it shouldn't be necessary for login:

webClient.setJavaScriptEnabled(false);


来源:https://stackoverflow.com/questions/4757233/using-htmlunit-to-log-into-facebook-programmatically-using-java

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