基本的なwebClient.getPageメソッドを使用して認証後にページを取得していますが、このWebサイトはある種のcomet / meteorサーバーを使用して、終了しないajaxリクエストを作成するため、getPageがループに入り、次のようになります。
2012年6月22日15:40:15com.gargoylesoftware.htmlunit.IncorrectnessListenerImplnotify警告:廃止されたコンテンツタイプが見つかりました:'application/x-javascript'。
javascriptを一度に無効にすると、ソースページが表示され、ハングしなくなります。
webClient.setJavaScriptEnabled(false);
しかし、JavaScriptイベントのあるボタンをクリックするなどのHtmlUnit機能を使用することはできません。私はこの問題に直面した最初の人ではないと思いますが、そこにまともな解決策を見つけることができないようです。
私が接続しようとしているページはFacebookです、これは私のコードです:
public static void submittingForm() throws Exception {
// final WebClient webClient = new WebClient();
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.setJavaScriptEnabled(true);
webClient.setTimeout(60000);
webClient.setRedirectEnabled(true);
webClient.setThrowExceptionOnFailingStatusCode(false);
webClient.setThrowExceptionOnScriptError(false);
webClient.setCssEnabled(false);
webClient.setUseInsecureSSL(true);
// Get the first page
final HtmlPage page1 = webClient.getPage("http://www.facebook.com");
// Get the form that we are dealing with and within that form,
// find the submit button and the field that we want to change.
final HtmlForm form = page1.getHtmlElementById("login_form");
final HtmlTextInput textFieldUsername = form.getInputByName("email");
final HtmlPasswordInput textFieldPassword = form.getInputByName("pass");
final HtmlSubmitInput button = form.getInputByValue("Log In");
// Change the value of the text field
textFieldUsername.setValueAttribute("emailhere/username");
textFieldPassword.setValueAttribute("password here");
// Now submit the form by clicking the button and get back the second page.
// And get the cookie set up.
final HtmlPage page2= button.click();
//Go to the bob marley fan page
HtmlPage fanPage = webClient.getPage("http://www.facebook.com/BobMarley");
webClient.setJavaScriptEnabled(true);
// Get the label that containes the like button from the fan page
HtmlLabel likeLabel = fanPage.getHtmlElementById("timelineHeadlineLikeButton");
try{
// Get the like button
HtmlSubmitInput likeButton = (HtmlSubmitInput)likeLabel.getLastChild();
// Press it
likeButton.click();
} catch (Exception e){
e.printStackTrace();
}
webClient.closeAllWindows();
}