Xpath:选择包含类及其特定子元素包含文本的 div [英] Xpath: select div that contains class AND whose specific child element contains text

查看:47
本文介绍了Xpath:选择包含类及其特定子元素包含文本的 div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

借助 this SO question 我有一个几乎可以正常工作的 xpath:

//div[contains(@class, 'measure-tab') and contains(., 'someText')]

然而,这有两个 div:一个是子 td 有 someText,另一个是子 span.

如何将范围缩小到带有 span 的范围?

<!-- 表 html 省略--><td>someText

<div class="measure-tab"><-- 我想选择这个 div(并使用 contains @class)<div><跨度>someText<-- 包含与此文本深度嵌套的跨度

解决方案

查找包含 span 在任何深度包含某些文本,请尝试:

//div[contains(@class, 'measure-tab') and contains(.//span, 'someText')]

也就是说,这个解决方案看起来非常脆弱.如果表格恰好包含带有您要查找的文本的 span,则包含表格的 div 也将匹配.我建议找到一种更强大的过滤元素的方法.例如,通过使用 ID 或顶级文档结构.

With the help of this SO question I have an almost working xpath:

//div[contains(@class, 'measure-tab') and contains(., 'someText')]

However this gets two divs: in one it's the child td that has someText, the other it's child span.

How do I narrow it down to the one with the span?

<div class="measure-tab">
  <!-- table html omitted -->
  <td> someText</td>
</div>

<div class="measure-tab">  <-- I want to select this div (and use contains @class)
  <div>
    <span> someText</span>  <-- that contains a deeply nested span with this text
  </div>
</div>

解决方案

To find a div of a certain class that contains a span at any depth containing certain text, try:

//div[contains(@class, 'measure-tab') and contains(.//span, 'someText')]

That said, this solution looks extremely fragile. If the table happens to contain a span with the text you're looking for, the div containing the table will be matched, too. I'd suggest to find a more robust way of filtering the elements. For example by using IDs or top-level document structure.

这篇关于Xpath:选择包含类及其特定子元素包含文本的 div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆