我有一个表,表中有这个:
<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表达式工作?
包含到期金额的<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")]