Reputation: 75
I am trying to simulate a search site travel ticket with HtmlUnit. The goal is to have the page Result of the search. My code returns the search page(waiting for the result ...)
Here is the code:
public class TestHtmlUnit {
public static void main(String[] args) throws Exception {
// Create and initialize WebClient object
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_10);
webClient.setThrowExceptionOnScriptError(false);
webClient.setRefreshHandler(new RefreshHandler() {
public void handleRefresh(Page page, URL url, int arg) throws IOException {
System.out.println("handleRefresh");
}
});
// visit Yahoo Mail login page and get the Form object
HtmlPage page = (HtmlPage) webClient.getPage("http://www.voyages-sncf.com/");
HtmlForm form = page.getFormByName("TrainTypeForm");
// Enter login and passwd of
form.getInputByName("origin_city").setValueAttribute("paris");
form.getInputByName("destination_city").setValueAttribute("marseille");
form.getInputByName("outward_date").setValueAttribute("28/03/2013");
// Click "Sign In" button/link
page = (HtmlPage) form.getInputByValue("Rechercher").click();
// Print the newMessageCount to screen
//System.out.println("newMessageCount = " + newMessageCount);
// System.out.println(page.asHTML());
System.out.println(page.asText());
}
}
Upvotes: 0
Views: 521
Reputation: 176
You should wait for the page to load after the click
try this
webClient.waitForBackgroundJavaScript(1000);
or
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.setAjaxController(new AjaxController(){
@Override
public boolean processSynchron(HtmlPage page, WebRequest request, boolean async)
{
return true;
}
});
or
JavaScriptJobManager manager = page.getEnclosingWindow().getJobManager();
while (manager.getJobCount() > 0) {
Thread.sleep(100);
}
Upvotes: 1