我正在使用基本的webClient.getPage方法在身份验证后检索页面,但是该网站使用某种彗星/流星服务器来发出永无止境的 ajax 请求,因此getPage进入循环,我得到:
2012 年 6 月 22 日下午 3:40:15 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl 通知警告:遇到过时的内容类型:'application/x-javascript'。
如果我一起禁用javascript,那么我会得到源页面并且它停止挂起:
webClient.setJavaScriptEnabled(false);
但是我不能使用 HtmlUnit 功能,例如单击具有 javascript 事件的按钮。我想我不是第一个遇到这个问题的人,但我似乎找不到一个像样的解决方案。
我要连接的页面是 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();
}