包含html标记内特定文本的任何html元素的Xpath

时间:2018-09-09 09:19:41

标签: xpath

需要找到与任何html标记中包含单词sidebar的html标记匹配的xpath。示例:

<p class='my class'>This is some text</p>
<h1 class='btn sidebar btn-now'><p>We have more text here</p><p> and anoter text here</p></div>
<div id='something here'>New text here</div>
<div id='something sidebar here'>New text again</div>
<nav class='this sidebar btn'>This is my nav</nav>
<sidebar><div>This is some text</div></sidebar>

我需要xpath来获取任何在<和结束> html标签之间具有单词“ sidebar”的html元素,无论是类,id还是html标签名称。在上面的示例中,我需要得到结果:

 <h1 class='btn sidebar btn-now'><p>We have more text here</p><p> and anoter text here</p></div>
<div id='something sidebar here'>New text again</div>
<nav class='this sidebar btn'>This is my nav</nav>
<sidebar><div>This is some text</div></sidebar>

需要是xpath而不是正则表达式

1 个答案:

答案 0 :(得分:2)

尝试以下方法,如果不是您要搜索的内容,请告诉我:

//*[contains(@*, "sidebar") or contains(name(), "sidebar")]
  • contains(@*, "sidebar")表示具有任何属性且包含"sidebar"
  • 的节点
  • contains(name(), "sidebar")-包含"sidebar"
  • 的节点名称

如果只需要idclass来包含"sidebar"

//*[contains(@id, "sidebar") or contains(@class, "sidebar") or contains(name(), "sidebar")]
相关问题