0

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());
    }
}
4

1 回答 1

1

您应该在点击后等待页面加载

尝试这个

webClient.waitForBackgroundJavaScript(1000);

或者

 webClient.setAjaxController(new NicelyResynchronizingAjaxController());
            webClient.setAjaxController(new AjaxController(){
                @Override
                public boolean processSynchron(HtmlPage page, WebRequest request, boolean async)
               {
                    return true;
                }
            });

或者

JavaScriptJobManager manager = page.getEnclosingWindow().getJobManager();
            while (manager.getJobCount() > 0) {
                Thread.sleep(100);
            }
于 2013-12-17T23:28:23.557 回答