2012-02-09 114 views
1

我有一個表,表中有這樣的:查找特定​​使用XPath

<table cellpadding="1" cellspacing="0" width="100%" border="0"> 
<tr> 

<td colspan="7" class="csoGreen" width="120%"><b class="white">Accounts and Payment Methods</b></td> 
</tr> 
<input type="hidden" name="pageNum" value=""/> 
<input type="hidden" name="reload" value=""/> 

<tr class="tableCellHeader"> 
<td nowrap="nowrap"><b></b></td> 
<td nowrap="nowrap"><b>Account Number 
<br/><br/><span id="accountToggle" style="color:#ff0000"></span></b></td> 
<td nowrap="nowrap"><b><center>Amount Due</center></b></td> 
<td nowrap="nowrap"><b><center>Due Date</center></b></td> 

<td nowrap="nowrap"><b>Enter/Edit<br/>Payment<br/>Amount<span class="red">*</span></b></td> 
<td nowrap="nowrap"><b>Enter/Edit<br/>Payment<br/>Date<span class="red">*</span></b></td> 
<td nowrap="nowrap"><b>Please select a Payment Method<span class="red">*</span></b></td> 

現在,我寫了這一點,它的工作原理:

account_overview_tag.xpath("//td[contains(descendant::center, 'Amount Due')]") 

不過,我想以測試它是一個具體的TD例如

account_overview_tag.xpath("//td[2][contains(descendant::center, 'Amount Due')]") 

如何讓我的XPATH表達式正常工作?

回答

1

包含您到期金額實際上是第三個孩子因此這款<td>

account_overview_tag.xpath("//td[2][contains(descendant::center, 'Amount Due')]") 

會給你一個nil表示沒有任何被匹配。然而,這個:

account_overview_tag.xpath("//td[3][contains(descendant::center, 'Amount Due')]") 

會給你你正在尋找的節點。你也可以通過在你的XPath中包含<tr>來做一些更具體的事情,例如:

//tr[@class="tableCellHeader"]/td[3][contains(descendant::center, "Amount Due")]