3

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?

Community
  • 1
  • 1
Robz
  • 1,747
  • 5
  • 21
  • 35

1 Answers1

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);
Pablojim
  • 8,542
  • 8
  • 45
  • 69
  • I have tried this but could not get logged into facebook. I get `page2` as https://www.facebook.com/?sk=welcome. If I enable javascript then I get the warning `WARN: (HtmlScript.java:472): Script is not JavaScript (type: application/ld+json, language: ). Skipping execution.` plus other warnings and errors. In the en I get `page2` as https://www.facebook.com/login.php?login_attempt=1&lwv=110. Can you please check out my question [Cannot login programmatically to facebook using htmlunit](http://stackoverflow.com/questions/34044808/cannot-login-programmatically-to-facebook-using-htmlunit) ? – Peter Dec 02 '15 at 16:56