Java HtmlUnit选择不在表单中的选项
希望使用HtmlUnit选择不在表单中的选项。当然,我需要检索结果页面。以下是我尝试过的:Java HtmlUnit选择不在表单中的选项,java,select,option,htmlunit,Java,Select,Option,Htmlunit,希望使用HtmlUnit选择不在表单中的选项。当然,我需要检索结果页面。以下是我尝试过的: public String getNewPage() throws Exception { try (final WebClient webClient = new WebClient()) { webClient.getOptions().setThrowExceptionOnScriptError(false); webClient.getOptions().
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;
}
单击某个选项时,页面会自动更新。在选择了正确的选项后,如何才能获得新页面?我所做的几乎是正确的,但缺少的是以下内容:
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());
你能提供url吗?谢谢,但我找到了解决问题的方法。我会把它贴在下面。