我已尝试过HTMLunit - Facebook Login和Using HTMLUnit to log into Facebook programmatically using Java中提供的代码。但是我没有登录Facebook。
启用javascript
webClient.setJavaScriptEngine(new JavaScriptEngine(webClient));
webClient.getOptions().setJavaScriptEnabled(true);
我得到了页面https://www.facebook.com/login.php?login_attempt=1&lwv=110。 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">]
禁用javascript
webClient.setJavaScriptEngine(new JavaScriptEngine(webClient));
webClient.getOptions().setJavaScriptEnabled(false);
我得到了页面https://www.facebook.com/?sk=welcome。 eclipse控制台中的输出:
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">]
源代码:
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();
}
任何帮助将不胜感激。谢谢!