前几天我正在试验HtmlUnit。我编写了一个程序来执行对站点的登录并收集一些信息。但是当单击特定按钮时,htmlUnit不会等待生成的操作。我试图等到JavascriptJobManager中的所有作业都完成但是它被卡在大约15个作业中。之后我试着等到最终的htmlpage改变,但这也不起作用。我接下来可以尝试什么?感谢您的时间,我会尽快实施任何建议。
编辑:我完全知道facebook不喜欢webcrawling,但我只是为了学习目的这样做,所以没有伤害。在错误消息之后,程序抛出。 http://www.pastebin.ca/3007578
单击infoButton时,会出现一个新窗口,使旧窗口无法访问。 http://imgur.com/aiF7nJR
final static WebClient webClient = new WebClient(BrowserVersion.FIREFOX_31);
public static void main(String [] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException, InterruptedException{
//init webclient
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.getOptions().setRedirectEnabled(true);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getCookieManager().setCookiesEnabled(true);
webClient.getOptions().setThrowExceptionOnScriptError(true);
webClient.getOptions().setCssEnabled(true);
webClient.getOptions().setUseInsecureSSL(true);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.waitForBackgroundJavaScript(12000);
webClient.setAlertHandler(new AlertHandler() {
@Override
public void handleAlert(Page arg0, String arg1) {
System.out.println("ALERT ON "+arg0.getUrl()+" :"+ arg1);
}
});
// perform the login
final HtmlPage loginPage = webClient.getPage("https://facebook.com");
final HtmlForm form = loginPage.getForms().get(0);
final HtmlTextInput username = form.getInputByName("email");
final HtmlPasswordInput password = form.getInputByName("pass");
final HtmlElement button = (HtmlElement) loginPage.getElementById("u_0_l");
username.setText("Your email");
password.setText("Your password");
final HtmlPage frontPage = (HtmlPage) button.click();
// The actual problem
final HtmlPage testPage = webClient.getPage("https://www.facebook.com/pages/Stackoverflow/1462865420609264");
HtmlElement infoButton = testPage.getFirstByXPath("//*[@class='share_action_link']"); // First share button.
HtmlPage testPage2 = infoButton.click();
JavaScriptJobManager manager = testPage2.getEnclosingWindow().getJobManager();
while (manager.getJobCount() > 0) {
Thread.sleep(1000);
webClient.waitForBackgroundJavaScript(100);
System.out.println(manager.getEarliestJob());
}
while(testPage == testPage2){
System.out.println("failed");
webClient.waitForBackgroundJavaScript(100);
Thread.sleep(5 * 1000);
}
}