用于包含给定文本节点的元素的XPath表达式

时间:2021-01-29 22:18:08

I am coding some Perl to use XPath to locate a particular td element within a table` that looks similar to this

我正在编写一些Perl来使用XPath来定位表格中的特定td元素,看起来与此类似

  <table>
    <tr>
      <td>...</td>
      <td><font color="white" face="verdana, Helvetica, Arial" size="2">Showing <b>1</b>-<b>100</b> of <b>200</b> total</font></td>
      <td>...</td>
      <td>...</td>
    </tr>
  </table>

What I want is to find a td element that has a font/text() node that contains the string Showing.

我想要的是找到一个td元素,其中包含字符串显示的字体/文本()节点。

matches contains(., "Showing")

匹配包含(。,“显示”)

A direct comparison works fine:

直接比较工作正常:

//td[font/text()="Showing "]

but I want to use the contains() XPath function so that the match is more flexible.

但我想使用contains()XPath函数,以便匹配更灵活。

I have tried

我努力了

//td[contains(font/text(), "Showing ")]

but this raises the error

但这会引发错误

XPath failed due to: A sequence of more than one item is not allowed as the first argument of contains()

and I have managed to achieve what I want with

我已经成功实现了我的目标

//td[font/text()[contains(., "Showing")]]

but this is very ugly and I am hoping for something more concise. Please can someone improve on this for me, or perhaps confirm that this is the best and most concise way?

但这非常难看,我希望有更简洁的东西。请有人可以为我改进,或者确认这是最好,最简洁的方法吗?

1 个解决方案

#1


7  

Try this:

尝试这个:

//td[contains(font/text()[1], 'Showing ')]

#1


7  

Try this:

尝试这个:

//td[contains(font/text()[1], 'Showing ')]