XPath 匹配表格中的文本 - Ruby - Nokigiri [英] XPath matching text in a table - Ruby - Nokigiri
问题描述
我有一张看起来像这样的桌子
I have a table that looks like this
<table cellpadding="1" cellspacing="0" width="100%" border="0">
<tr>
<td colspan="9" class="csoGreen"><b class="white">Bill Statement Detail</b></td>
</tr>
<tr style="background-color: #D8E4F6;vertical-align: top;">
<td nowrap="nowrap"><b>Bill Date</b></td>
<td nowrap="nowrap"><b>Bill Amount</b></td>
<td nowrap="nowrap"><b>Bill Due Date</b></td>
<td nowrap="nowrap"><b>Bill (PDF)</b></td>
</tr>
</table>
我正在尝试创建 XPATH 以查找包含测试 Bill Statement Detail 的表.我想要整个表,而不仅仅是 td.
I am trying to create the XPATH to find this table where it contains the test Bill Statement Detail. I want the entire table and not just the td.
这是我迄今为止尝试过的:
Here is what I have tried so far:
page.parser.xpath('//table[contains(text(),"Bill")]')
page.parser.xpath('//table[contains(text(),"Bill")]')
page.parser.xpath('//table/tbody/tr[contains(text(),"账单明细")]')
page.parser.xpath('//table/tbody/tr[contains(text(),"Bill Statement Detail")]')
感谢任何帮助
谢谢!
推荐答案
您的第一个 XPath 示例与您选择的 table
最接近.第二个示例,如果它匹配,将选择 tr
—这将不起作用,主要是因为根据您的示例,您想要的文本位于 b
节点中,不是 tr
节点.
Your first XPath example is the closest in that you're selecting table
. The second example, if it ever matched, would select tr
—this one will not work mainly because, according to your example, the text you want is in a b
node, not a tr
node.
由于*
,这个解决方案是我所能做到的含糊不清.如果目标文本始终在 b
下,请将其更改为 descendant::b
:
This solution is as vague as I could make it, because of *
. If the target text will always be under b
, change it to descendant::b
:
//table[contains(descendant::*, 'Bill Statement Detail')]
举个例子,这是我能做到的:
This is as specific, given the example, as I can make:
//table[tr[1]/td/b['Bill Statement Detail']]
这篇关于XPath 匹配表格中的文本 - Ruby - Nokigiri的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!