XPath定位元素使用text()不起作用

4
<td>
 <a>SAMPLE</a>
 <div>
  <small>Phase:
         Planning >>
  <a>Performance</a>
  </small>
 </div>         
</td>

我正在试图定位上面带有“阶段:规划>>性能”文本的元素,但一直没有成功。

尝试使用:

//*[text()[contains(normalize-space(.),'Phase: Planning >> Performance')]]
4个回答

3

目前存在的其他三个答案都不能完全满足您的要求。

以下是一些XPath表达式,可以实现您的要求:

  • You can select the small elements whose normalized string value is Phase: Planning >> Performance via this XPath:

    //small[normalize-space() = 'Phase: Planning >> Performance']
    
  • You can select all elements, regardless of name whose normalized string values equals the targeted string:

    //*[normalize-space() = 'Phase: Planning >> Performance']
    

    but this will select not only small but also div because both have a string value equal to Phase: Planning >> Performance.

  • You can select only the lowest element in the hierarchy, regardless of name whose normalized string values equals the targeted string:

    //*[         normalize-space() = 'Phase: Planning >> Performance' and 
        not(.//*[normalize-space() = 'Phase: Planning >> Performance'])]
    

重要的是要意识到XPath text()与XPath . =不同

0

请尝试以下:

//small[contains(.,'Phase:')]

希望能对你有所帮助 :)

0
<span>Planning &gt;&gt;</span>

试试这段代码


0

文本 Phase: Planning >>Performance 属于两个不同的元素。

要定位 Phase: Planning >> 元素,您可以使用

//small[contains(text(),'Phase:')]

并定位到Performance元素

//a[contains(text(),'Performance')]

请注意,打印第一个元素将会给出两个元素Phase: Planning >> Performance的文本。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接