![](/img/trans.png)
[英]How to submit a form using Jsoup without the submit button having a name?
[英]how to submit form using jsoup
我想從具有多個表單的網頁提交表單。 我要特別在下面提交此表格。
<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();
我希望能夠在提交此表單后查看出現的網頁,還可以查看該頁面的文本內容,並且還希望獲得網址。
我必須在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();
首先,您需要調查頁面本身。 Chrome具有出色的內置功能來支持它。 只需按F12鍵,您將看到很少的選項,並且您可以調查幾乎與特定HTML請求相關的所有內容。 較新版本的IE也具有類似的功能。 Firefox具有強大的Firebug擴展。
您可以看到服務器使用的Cookies。 也許你想念他們。 通常最重要的一個是Set-Cookie。 您需要將其添加到您將在POST HTML請求中發送的Cookie Post請求中。
對於您的情況,您還需要檢查Java腳本函數SubmitForm。 請記住,您的代碼可能不會調用JavaScript。 您需要向其發送請求的頁面將是“ realDisplay.asp”。
有時,瀏覽器發送帶有URL字符串中某些參數的發布請求,例如GET。 請檢查它們。 使您的POST請求與瀏覽器所使用的完全相同。
通過瀏覽器登錄時,請檢查會話多長時間。 要保持會話狀態,您將需要向應用程序發送一些請求。 如果未發送,則需要重新登錄。
還有將jsoup與cookie一起使用的答案。
Connection.Response res = Jsoup.connect("http://www.example.com/login.php")
.data("username", "myUsername", "password", "myPassword")
.method(Method.POST)
.execute();
Document doc = res.parse();
如果您需要更多信息,請告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.