【问题标题】:Select substring of text from span and compare with number从范围中选择文本的子字符串并与数字进行比较
【发布时间】:2020-08-14 21:38:07
【问题描述】:

我有以下html

<div _something="">
    <div _something="" class="class12928">
        <span _something="" class="ux-u-font-bold">Search results</span>
    </div>
    <div _something="" class="class12928">
        <span _something="" class="ux-u-font-light">329 item(s) found</span>
    </div>
</div>

数字 329 是通过 rest 调用生成的,最初为 0,因此初始负载显示如下:

<span _something="" class="ux-u-font-light">0 item(s) found</span>

我想通过 xpath 检查其余调用是否返回大于 0 的数字。

我通过以下 xpath 获得了跨度: //span[contains(text(),"item")]

我想搜索任何数量超过 0 的项目

【问题讨论】:

  • 如果您的文本部分是常量item(s) found,则只需获取所需跨度的文本并将item(s) found替换为'',然后解析剩余的字符串部分以获取数字。
  • 我认为关键是在 rest 调用结束后进行解析操作。

标签: html dom xpath


【解决方案1】:

使用 XPath,

选择包含符合您条件的 span 元素的 div 元素(如果您只想要 span,请删除最后一部分 /parent::div):

//span[contains(.,"found")][substring-before(.," item(s)")>0]/parent::div

检查 span 元素是否包含大于 0 的数字(返回 TRUE/FALSE):

substring-before(//span[contains(.,"found")]," item(s)")>0

【讨论】:

  • 第一个工作://span[contains(.,"found")][substring-before(.," item(s)")&gt;0]
【解决方案2】:
//span [@class = 'ux-u-font-light' and contains(., 'item(s) found') and not (starts-with(., '0')) ]

【讨论】:

  • 这甚至更短的版本也有效://span[contains(., 'item(s) found') and not (starts-with(., '0')) ]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-20
  • 1970-01-01
  • 1970-01-01
  • 2012-08-31
相关资源
最近更新 更多