Html XPath 选择包含指定文本的单元格的表格行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4608097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XPath to select a table row that has a cell containing specified text
提问by KClough
How do I select a table row that has a cell containing specified text with XPath?
如何使用 XPath 选择具有包含指定文本的单元格的表格行?
回答by Dimitre Novatchev
Use:
使用:
ExpressionSelectingTable/tr[td//text()[contains(., 'targetString')]]
This means:
这意味着:
Select every tr
that is a child of any table selected by the expression ExpressionSelectingTable
and that (the tr
) has at least one td
child that has at least one text-node descendent that contains the string 'targetString'
选择tr
作为表达式选择的任何表的子项的每个子项,ExpressionSelectingTable
并且 (the tr
) 至少有一个td
子项,该子项至少有一个包含字符串的文本节点后代'targetString'
回答by stefan.natchev
To select rows with cells containing some text you would use this XPath expression:
要选择包含某些文本的单元格的行,您可以使用此 XPath 表达式:
//tr/td[normalize-space(text())="Banana"]/..
//tr/td[normalize-space(text())="Banana"]/..
This selects any td
that contains text "Banana" and then selects the parent with /..
这将选择td
包含文本“Banana”的任何内容,然后使用/..