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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 05:58:28  来源:igfitidea点击:

XPath to select a table row that has a cell containing specified text

htmlxpath

提问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 trthat is a child of any table selected by the expression ExpressionSelectingTableand that (the tr) has at least one tdchild 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 tdthat contains text "Banana" and then selects the parent with /..

这将选择td包含文本“Banana”的任何内容,然后使用/..