【问题标题】:Xpath for any html element containing specific text inside html tag包含 html 标签内特定文本的任何 html 元素的 Xpath
【发布时间】:2018-09-09 09:19:41
【问题描述】:

需要找到与任何 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 而不是正则表达式

【问题讨论】:

  • 是否也能找到&lt;a href="sidebar"&gt;&lt;div data-sidebar="foo"&gt;&lt;p class="sideb&amp;#97;r"&gt;
  • @melpomene 是的,html 标签里面有侧边栏
  • 最后一个的 HTML 代码中没有sidebar
  • 标签(不是有效的 html 标签,仅作为示例)但基本上如果它存在,那么 之间的所有内容都应该匹配。如果您认为那里不正常,我可以从上面的示例中删除它
  • 我说的是我的例子,不是你的。 &lt;p class="sideb&amp;#97;r"&gt; 不包含 sidebar。你希望它被发现还是不被发现?

标签: xpath


【解决方案1】:

试试下面,如果不是您要搜索的内容,请告诉我:

//*[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")]

【讨论】:

  • 这是否同时检查属性名称和属性值?
  • 仅属性值。您还需要属性名称吗?如果是,添加or contains(@*/name(), "sidebar")谓词
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-11
  • 2010-10-26
  • 2021-10-09
  • 1970-01-01
  • 1970-01-01
  • 2014-06-01
  • 2012-01-13
相关资源
最近更新 更多