【发布时间】:2013-09-18 14:07:52
【问题描述】:
我正在使用 HTMLUnit 获取网页上的页面。在这个网页中,有一个表格。当我从 Chrome 加载并查看源代码时:类似这样:
<form name="form" method="post" onsubmit="return checkDate();">
<input name="check_in_date" id="check_in_date" readonly="readonly" type="text" class="hasDatepicker"/>
<input name="check_out_date" id="check_out_date" readonly="readonly" type="text" class="hasDatepicker"/>
<input name="check_availability" value="test condition" type="submit"/>
</form>
但是当我通过这段代码使用 HTMLUnit 加载时:
String url = "sample link";
WebClient webClient = new WebClient();
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setCssEnabled(false);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
HtmlPage page = webClient.getPage(url);
System.out.println(page.asXML());
我得到不同的 HTML 代码。更多细节:
<form name="form" method="post" onsubmit="return checkDate();">
<input name="check_in_date" id="check_in_date" readonly="readonly" type="text" class="hasDatepicker"/>
<input name="check_out_date" id="check_out_date" readonly="readonly" type="text" class="hasDatepicker"/>
<input name="check_availability" value="test condition" type="text"/>
</form>
这里不同的是:最后一行:<input name="check_availability" value="test condition" type="text"/> 现在输入文本,而不是提交,所以我不能这样的代码:
HtmlForm form = page.getFormByName("form");
HtmlSubmitInput submit = form.getInputByName("check_availability"); // error at this line
page = submit.click();
错误,因为现在这个字段不再是button,它只是一个文本`。我不知道为什么会有这种差异。请告诉我如何解决这个问题。
谢谢:)
【问题讨论】: