我想从有多个表单的网页提交表单。我特别想在下面提交此表格。
<form action="realDisplay.asp" method="post" name="Search" onSubmit="return validate(this); return submitForm();" target="_blank">
<table width="98%" align="center" cellspacing="0" cellpadding="0" border="1" bordercolor="#FFFFFF">
<tr>
<td width="127" class="style62" align="right">Parcel ID</td>
<td width="286">
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" width="100%%" align="center">
<tr>
<td align="left" class="style62">
<input name="rePID" size="15" maxlength="15" value="">
<br>
<font class="style65">( 12 123 12 123)</font></td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td width="127" class="style62" align="right">Partial Parcel ID</td>
<td width="286">
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" width="100%%" align="center">
<tr>
<td align="left" class="style62">
<input name="rePartialPID" size="15" maxlength="15" value="">
<br>
<font class="style65">( 12 123)</font></td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td class="style62" align="right">Address</td>
<td class="style62" align="left">
<input name="Address" size="38" maxlength="50" value="">
</td>
</tr>
<tr>
<td class="style62" align="right" nowrap="nowrap">Partial Street Name</td>
<td class="style62" align="left">
<input name="streetName" size="38" maxlength="50" value="">
</td>
</tr>
<tr>
<td class="style62" align="right" nowrap="nowrap">Owner Name</td>
<td class="style62" align="left">
<input name="OwnerName" size="30" maxlength="50" value="">
</td>
</tr>
<tr>
<td colspan="2"><br /><font class="style64">Insert</font>
<font class="style65"><u>Either</u></b></font><font class="style64"> a</font>:<br>
<br>
<table align="center" border="1" cellspacing="0" cellpadding="3" bgcolor="#E8E8E8">
<tr>
<td align="left"><img src="Images/arrow_1.gif" width="9" height="7" vspace="0" hspace="8"></td>
<td class="style75" align="left">
<a class="nav4" href="GlossaryTermWin.htm#ParcelID" onClick="NewWindow(this.href,'PARCELID','635','635','yes');return false;">
Parcel ID</a> , or Partial Parcel ID</td>
</tr>
<tr>
</tr>
<tr>
<td>
<img src="Images/arrow_1.gif" width="9" height="7" vspace="0" hspace="8"></td>
<td class="style75" align="left">Address (eg. 123 main), or</td>
</tr>
<tr>
<td class="style75" align="left">
<img src="Images/arrow_1.gif" width="9" height="7" vspace="0" hspace="8"></b></font></td>
<td class="style75">Partial Street Name (eg. main), or</td>
</tr>
<tr>
<td class="style75">
<img src="Images/arrow_1.gif" width="9" height="7" vspace="0" hspace="8"></b></font></td>
<td class="style75" align="left">Owner Name <br />(eg. LastName,FirstName <br />or
Partial Owner Name)</td>
</tr>
</table> </td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td height="26" colspan="2" align="center">
<input type="image" valign="top" name="Submit" value="Search" src="images/search.jpg" align="top" alt="Search by either the Parcel ID or Address or Owner Name that is associated by the real estate information.">
<a href="javascript:document.forms[0].reset()" border="0"><img src="images/reset.jpg" align="top" border="0" alt="Reset the values on this page." onClick="ResetForm()"></a></td>
</tr>
</table>
</form></td>
我的 Java 代码如下所示:
Document doc = Jsoup.connect("http://web.somewebsite.asp")
.data("rePID", "15 197 14 007")
.post();
我希望能够查看提交此表单后出现的网页,并查看该页面的文本内容,并且还希望获取 url。
我必须在我的 Java 代码中做什么以确保提交请求已提交并在提交表单后查看下一页的内容。
我可以使用 Htmlunit 提交表单,您可以在网上搜索并下载包。
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlImageInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_10);
final HtmlPage searchPage = webClient.getPage("http://web.somewebsite.asp");
final HtmlForm form = searchPage.getFormByName("Search");
final HtmlTextInput textField = form.getInputByName("rePID");
textField.setValueAttribute("15 197 14 007");
final HtmlImageInput button = form.getInputByName("Submit");
HtmlPage searchResultPage = (HtmlPage)button.click();