Cannot login programmatically to facebook using htmlunit

懵懂的女人 提交于 2019-12-20 01:42:11

问题


I have tried the code given in HTMLunit - Facebook Login and Using HTMLUnit to log into Facebook programmatically using Java. However I am not logged into facebook.

With javascript enabled

webClient.setJavaScriptEngine(new JavaScriptEngine(webClient));
webClient.getOptions().setJavaScriptEnabled(true);

I get the page https://www.facebook.com/login.php?login_attempt=1&lwv=110. Also several warnings and errors are reported by htmlunit:

WARN:  (HtmlScript.java:472): Script is not JavaScript (type: application/ld+json, language: ). Skipping execution.
WARN:  (IncorrectnessListenerImpl.java:38): Obsolete content type encountered: 'application/x-javascript'.
INFO:  (InputElementFactory.java:182): Bad input type: "email", creating a text input
WARN:  (IncorrectnessListenerImpl.java:38): Obsolete content type encountered: 'application/x-javascript'.
WARN:  (IncorrectnessListenerImpl.java:38): Obsolete content type encountered: 'application/x-javascript'.
WARN:  (IncorrectnessListenerImpl.java:38): Obsolete content type encountered: 'application/x-javascript'.
ERROR: (StrictErrorReporter.java:82): runtimeError: message=[TypeError: Cannot convert a Symbol value to a string] sourceName=[https://static.xx.fbcdn.net/rsrc.php/v2/yy/r/Gc8wm2svwWB.js] line=[36] lineSource=[null] lineOffset=[0]
WARN:  (IncorrectnessListenerImpl.java:38): Obsolete content type encountered: 'application/x-javascript'.
WARN:  (IncorrectnessListenerImpl.java:38): Obsolete content type encountered: 'application/x-javascript'.
WARN:  (IncorrectnessListenerImpl.java:38): Obsolete content type encountered: 'application/x-javascript'.
WARN:  (IncorrectnessListenerImpl.java:38): Obsolete content type encountered: 'application/x-javascript'.
WARN:  (IncorrectnessListenerImpl.java:38): Obsolete content type encountered: 'application/x-javascript'.
WARN:  (IncorrectnessListenerImpl.java:38): Obsolete content type encountered: 'application/x-javascript'.
WARN:  (IncorrectnessListenerImpl.java:38): Obsolete content type encountered: 'application/x-javascript'.
WARN:  (IncorrectnessListenerImpl.java:38): Obsolete content type encountered: 'application/x-javascript'.
WARN:  (IncorrectnessListenerImpl.java:38): Obsolete content type encountered: 'application/x-javascript'.
WARN:  (IncorrectnessListenerImpl.java:38): Obsolete content type encountered: 'application/x-javascript'.
WARN:  (IncorrectnessListenerImpl.java:38): Obsolete content type encountered: 'application/x-javascript'.
WARN:  (IncorrectnessListenerImpl.java:38): Obsolete content type encountered: 'application/x-javascript'.
WARN:  (IncorrectnessListenerImpl.java:38): Obsolete content type encountered: 'application/x-javascript'.
WARN:  (IncorrectnessListenerImpl.java:38): Obsolete content type encountered: 'application/x-javascript'.
WARN:  (IncorrectnessListenerImpl.java:38): Obsolete content type encountered: 'application/x-javascript'.
1st page url: https://www.facebook.com/
1st page title: Facebook - Log In or Sign Up
1st page body: HtmlBody[<body class="fbIndex UIPage_LoggedOut _2gsg _xw8 _5p3y hasBanner gecko win x1 Locale_en_US" dir="ltr">]
WARN:  (IncorrectnessListenerImpl.java:38): Obsolete content type encountered: 'application/x-javascript'.
ERROR: (StrictErrorReporter.java:82): runtimeError: message=[TypeError: Cannot convert a Symbol value to a string] sourceName=[https://static.xx.fbcdn.net/rsrc.php/v2/yy/r/Gc8wm2svwWB.js] line=[36] lineSource=[null] lineOffset=[0]
2nd page url: https://www.facebook.com/login.php?login_attempt=1&lwv=110
2nd page title: Log into Facebook | Facebook
2nd page body: HtmlBody[<body class="login_page UIPage_LoggedOut _2gsg _xw8 _5p3y gecko win x1 Locale_en_US" dir="ltr">]

With javascript disabled

webClient.setJavaScriptEngine(new JavaScriptEngine(webClient));
webClient.getOptions().setJavaScriptEnabled(false);

I get the page https://www.facebook.com/?sk=welcome. The output in eclipse console:

INFO:  (InputElementFactory.java:182): Bad input type: "email", creating a text input
INFO:  (InputElementFactory.java:182): Bad input type: "email", creating a text input
1st page url: https://www.facebook.com/?_fb_noscript=1
1st page title: Facebook - Log In or Sign Up
1st page body: HtmlBody[<body class="fbIndex UIPage_LoggedOut _2gsg _xw8 _5p3y hasBanner gecko win x1 Locale_en_US" dir="ltr">]
2nd page url: https://www.facebook.com/?sk=welcome
2nd page title: Facebook
2nd page body: HtmlBody[<body class="hasLeftCol home composerExpanded _5vb_ fbx _5p3y gecko win x1 Locale_en_US" dir="ltr">]

The source code:

final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_38);
webClient.setJavaScriptEngine(new JavaScriptEngine(webClient));
webClient.getOptions().setJavaScriptEnabled(false);
HtmlPage page1 = null;
try {
    page1 = webClient.getPage("http://www.facebook.com");
    System.out.println("1st page url: " + page1.getUrl());
    System.out.println("1st page title: " + page1.getTitleText());
    System.out.println("1st page body: " + page1.getBody());
} catch (IOException e) {
    e.printStackTrace();
}
final HtmlForm form = (HtmlForm) page1.getElementById("login_form");
final HtmlSubmitInput button = (HtmlSubmitInput) form.getInputsByValue("Log In").get(0);
final HtmlTextInput textField = (HtmlTextInput) page1.getElementById("email");
textField.setValueAttribute("myaddress@test.com");
final HtmlPasswordInput textField2 = (HtmlPasswordInput) page1.getElementById("pass");
textField2.setValueAttribute("mypassword");
try {
    HtmlPage page2 = button.click();
    System.out.println("2nd page url: " + page2.getUrl());
    System.out.println("2nd page title: " + page2.getTitleText());
    System.out.println("2nd page body: " + page2.getBody());
} catch (IOException e) {
    e.printStackTrace();
}

Any help would be appreciated. Thank you!

来源:https://stackoverflow.com/questions/34044808/cannot-login-programmatically-to-facebook-using-htmlunit

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