【问题标题】:Select a div for a specific class which has child elements that contains a certain text为具有包含特定文本的子元素的特定类选择一个 div
【发布时间】:2020-08-26 23:36:05
【问题描述】:

鉴于此 html 示例:

<div class="measure-tab"> --- i want to select this one
  <span class="color_title">someText</span>
</div>
<div class="measure-tab"> --- i dont want to select this one
  <span class="color_title">someText</span>
  <div>
      <span class="qwery">jokerText</span>
  </div>
</div>
<div class="measure-tab"> --- i want to select this one
  <span class="color_title">someText</span>
</div>

我想选择具有 @class='measure-tab' 的 div,它下面有一个作为特定类和 text = someText 的跨度和一个具有特定类但不包含 text = ' 的嵌套跨度jokerText',所有这些都在 XPATH 中

我试过的是:

//div[contains(@class, 'measure-tab') and //span[@class="color_title" and (contains(text(),'someText')) and //span[@class="color_title" and not(contains(text(),'jokerText'))]]

但这似乎不起作用。 我还使用This 帖子作为灵感。

编辑:更正了对该问题目标的错误描述

编辑,提出了一个新的解决方案:

//div[contains(@class, 'measure-tab') and //span[contains(@class, 'color_title') and //span[not(contains(@class, 'qwery'))]]]

但这会返回所有 div,而不是不匹配 --- 我不想选择这个

<span class="color_title">someText</span>
  <div>
      <span class="qwery">jokerText</span>
  </div>

我感觉如此接近,但到目前为止,哈哈,这对我来说没有意义,为什么当我写的时候它与&lt;span class="qwery"&gt;jokerText&lt;/span&gt; 匹配不包含那里

【问题讨论】:

  • 以下答案是否解决了您的问题?如果是,请随时点击图标√ 接受正确答案

标签: python selenium xpath


【解决方案1】:

我相信这就是你要找的-

MyDivs = driver.find_elements_by_xpath("//div[@class='measure-tab' and not(descendant::*[text() = 'jokerText' and @class = 'qwery'])]")

这将选择所有没有jokerText 的div 标签。

【讨论】:

  • 是的,但这不会选择有&lt;span class="qwery"&gt;jokerText&lt;/span&gt;的div吗?
  • 是的,这就是我所做的,但是它确实与许多其他类似的东西混在一起,所以我真的想通过 xpath 来调整它,谢谢你的努力,如果没有其他人可以帮助我也可以选择你的答案
  • 我还使用仅通过标签属性查看的新解决方案编辑了帖子,但似乎不包含不做我希望它做的事情,即没有获得具有 attr 的 div @class="qwery"
  • 我已经更改了我的代码并尝试满足您的所有要求,希望它有效:(.
【解决方案2】:

您可以通过not(following-sibling::div/span.....)

查询

尝试使用以下 xpath:

//span[@class='color_title' and not(following-sibling::div/span[@class='qwery' and text()='jokerText'])]/parent::div[@class='measure-tab']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-20
    • 2015-10-16
    • 1970-01-01
    • 2011-07-14
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多