gpt4 book ai didi

java - 使用 htmlunit 检索结果页面

转载 作者:行者123 更新时间:2023-12-01 14:43:54 25 4
gpt4 key购买 nike

我正在尝试使用 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());
}
}

最佳答案

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

试试这个

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

关于java - 使用 htmlunit 检索结果页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15661259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com