html - XPath在子元素中测试文本?

标签 html xml xpath

我有类似表格格式的HTML(不是<table>)。我需要检查给定的row是否具有所有值。

...
<div id="rows">
  <div class="row">
    <p>Java</p>
    <p>Ruby</p>
  </div>
  <div class="row">
    <p>HTML</p>
    <p>Ruby</p>
  </div>
</div>
...


我如何使用xpath检查单个Java AND Ruby中是否有row

我尝试了//p[text()='Java' and text()='Ruby'],但工作不充分。还尝试了//div[@id='rows']//p[contains(text(),'Java') and contains(text(),'Ruby')]

最佳答案

尝试使用以下xpath:-

//div[normalize-space(.) = 'Java Ruby']


要么

//div[@class = 'row' and contains(.,'Java') and contains(.,'Ruby')]


或如果您想要ID为div且包含多个ID的rows,请尝试以下方法:-

//div[@id= 'rows' and contains(.,'Java') and contains(.,'Ruby')]

关于html - XPath在子元素中测试文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39122118/

相关文章:

jquery - 使用 jQuery 显示数组中的所有项目

c# - 关于构建/格式化 LINQ to XML 查询的最佳方式的建议?

javascript - 是否可以获取元素的 css?

c# - 如何使用 XPathNavigator 并返回不同的节点?

javascript - 删除 div 时快速滚动同级 div 的方法

html - Internet Explorer 不呈现从 JQuery ajax post 返回的 html

html - 为什么这个 span 不包含这些 SVG 元素?

xml - 在 XSLT v1.0 中使用结尾

android - 行分隔符在 ScrollView Android 中不起作用

python - XPath:删除空格功能不起作用