想使用HtmlUnit选择不在表单内的选项。然后我需要检索结果页面。这是我试过的:
public String getNewPage() throws Exception {
try (final WebClient webClient = new WebClient()) {
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setPopupBlockerEnabled(true);
webClient.getOptions().setJavaScriptEnabled(true);
final HtmlPage page = webClient.getPage(URL);
HtmlOption option1 = (HtmlOption) page.getElementById("1");
option1.removeAttribute("selected");
HtmlOption option5 = (HtmlOption) page.getElementById("5");
option5.setSelected(true);
// Some code missing here........
return newHtmlString;
}
单击选项时,页面会自动更新。如何在选择了正确的选项后获取新页面?
答案 0 :(得分:1)
我所做的几乎是正确的,但遗漏的是:
page.refresh();
return page.asXml();
然后我有另一个问题是将复选框标记为已选中。这对我有用:
HtmlCheckBoxInput checkbox = (HtmlCheckBoxInput) page.getElementById("cb4");
checkbox.setAttribute("checked", "checked");
checkbox.fireEvent(Event.TYPE_CHANGE);
page.refresh();
System.out.println(page.asXml());