0
我試圖用HtmlUnit模擬一個搜索網站旅行票。目標是獲得搜索結果頁面。我的代碼返回搜索頁面(等待結果......)用htmlunit檢索結果頁面
下面是代碼:
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());
}
}