Using HTMLUnit to login to Facebook programmatically using Java

This question is an additional complement to this question: How to log in to Facebook using Java with software?

I used (a slightly modified version) the following code to log into the accounts of other sites 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, when I try to login to facebook with the correct email and password, I have 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?

+3
source share
1 answer

It seems htmlunit doesn't like some kind of javascript.

Try disabling it, as it is not necessary to enter:

webClient.setJavaScriptEnabled(false);
+1
source

Source: https://habr.com/ru/post/1786872/


All Articles