首页 > 解决方案 > 使用 xPath 在文本中查找具有特定单词的跨度

问题描述

我有很多不同文本的跨度。但是我需要在这个文本中找到一个带有某个单词的长文本的跨度。

<div class="commentMessage">
  <span>Any endgame spoilers</span>
</div>

在这种情况下,我想选择一个包含带有某个单词“endgame”的文本的跨度。

这是我尝试过的:

//div[@class='commentMessage']/span[text()='endgame'] - but it show no results (very strange) =\

然后我尝试使用所有短语找到跨度:

//div[@class='commentMessage']/span[text()='Any endgame spoilers'] - and it works =\

但正如我所说,我需要找到一个使用某个单词的跨度。

另外,我尝试了这种结构:

//div[@class='commentMessage']/span[contains(text(), 'endgame')] - but Chrome xPath extension says: Type is not appropriate for the context in which the expression occurs

标签: htmlxmlseleniumxpath

解决方案


尝试以下 xpath。

 //div[@class='commentMessage']/span[contains(.,'endgame')]

推荐阅读