java - 使用 xpath 仅在第一个 div 中获取 span 标签的计数
问题描述
我想 <span class="show-for-sr">
单独计算第一个 div 中的数量。
我当前的 xpath 正在返回跨多个 div 的跨度计数。我尝试了以下两个 xpath,但它们不起作用。为什么当我通过 [1] 来表示第一个 div 时,它仍然会计算所有 div 元素的跨度?
1.
count(//div[@class='rating-container']//span[@class='show-for-sr'])
count(//div[@class='rating-container'][1]//span[@class='show-for-sr'])
HTML:
<div>
<div class="cell small-12 medium-12 large-6">
<div class="rating-container">
<span class="text-blue-500 mr-8">
<div class="tt-container"><button
aria-label="open tooltip" class="button link" type="button" role="tooltip"
aria-expanded="false">Morningstar rating</button></div>
</span>
<span><span
class="icon fil-icon star-grey fil-icon-star-filled"><span class="show-for-sr">star-filled</span></span><span
class="icon fil-icon star-grey fil-icon-star-filled"><span class="show-for-sr">star-filled</span></span><span
class="icon fil-icon star-grey fil-icon-star-filled"><span class="show-for-sr">star-filled</span></span><span
class="icon fil-icon star-grey fil-icon-star-filled"><span class="show-for-sr">star-filled</span></span><span
class="icon fil-icon star-grey fil-icon-star-filled"><span class="show-for-sr">star-filled</span></span></span>
</div>
</div>
<div class="cell mb-32">
<p class="intro morningstarInfo text-center-mob">Morningstar
Rating<sup>TM</sup> (relative to category)
</p>
<table class="table striped" id="morningstar-info-table">
<thead class="">
<tr>
<th scope="col" id="year" class="">Year</th>
<th scope="col" id="morningstar return" class="">Morningstar return</th>
<th scope="col" id="morningstar risk" class="">Morningstar risk</th>
<th scope="col" id="morningstar rating" class="">Morningstar rating</th>
</tr>
</thead>
<tbody>
<tr>
<td>3 - year</td>
<td>High</td>
<td>Low</td>
<td>
<div class="rating-container"><span
class="text-blue-500 mr-8"> </span><span><span
class="icon fil-icon star-grey fil-icon-star-filled"><span
class="show-for-sr">star-filled</span></span><span
class="icon fil-icon star-grey fil-icon-star-filled"><span
class="show-for-sr">star-filled</span></span><span
class="icon fil-icon star-grey fil-icon-star-filled"><span
class="show-for-sr">star-filled</span></span><span
class="icon fil-icon star-grey fil-icon-star-filled"><span
class="show-for-sr">star-filled</span></span><span
class="icon fil-icon star-grey fil-icon-star-filled"><span
class="show-for-sr">star-filled</span></span></span></div>
</td>
</tr>
<tr>
<td>5 - year</td>
<td>High</td>
<td>Low</td>
<td>
<div class="rating-container"><span
class="text-blue-500 mr-8"> </span><span><span
class="icon fil-icon star-grey fil-icon-star-filled"><span
class="show-for-sr">star-filled</span></span><span
class="icon fil-icon star-grey fil-icon-star-filled"><span
class="show-for-sr">star-filled</span></span><span
class="icon fil-icon star-grey fil-icon-star-filled"><span
class="show-for-sr">star-filled</span></span><span
class="icon fil-icon star-grey fil-icon-star-filled"><span
class="show-for-sr">star-filled</span></span><span
class="icon fil-icon star-grey fil-icon-star-filled"><span
class="show-for-sr">star-filled</span></span></span></div>
</td>
</tr>
<tr>
<td>Overall</td>
<td>High</td>
<td>Low</td>
<td>
<div class="rating-container"><span
class="text-blue-500 mr-8"> </span><span><span
class="icon fil-icon star-grey fil-icon-star-filled"><span
class="show-for-sr">star-filled</span></span><span
class="icon fil-icon star-grey fil-icon-star-filled"><span
class="show-for-sr">star-filled</span></span><span
class="icon fil-icon star-grey fil-icon-star-filled"><span
class="show-for-sr">star-filled</span></span><span
class="icon fil-icon star-grey fil-icon-star-filled"><span
class="show-for-sr">star-filled</span></span><span
class="icon fil-icon star-grey fil-icon-star-filled"><span
class="show-for-sr">star-filled</span></span></span></div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
解决方案
代替
count(//div[@class='rating-container'][1]//span[@class='show-for-sr'])
用这个
count((//div[@class='rating-container'])[1]//span[@class='show-for-sr'])
您应该将整个 div 包装在索引中。
推荐阅读
- google-sheets - 包含包含的 IMPORTXML Xpath(Google 表格)
- reactjs - 将数据道具传递给函数对象
- r - 根据百分比值将值分组为分位数并在 R 中可视化?
- rxjs - RxJs:使用另一个可观察对象的发射过滤可观察对象中的内容
- swift - 如何在 Swift 中创建具有多个参数的通用记忆函数?
- java - JPA @ManyToOne 单向:删除父级时如何将 FK 设置为 null 并保留子实体
- azure - 将设置或配置文件添加到 TFS 扩展开发
- algorithm - 具有大方差/接近点的 k-means 成本函数的问题以及如何解决它
- php - move_uploaded_file(/test.txt.part1):打开流失败:权限被拒绝
- node.js - 使用 NodeJ 进行 AWS DynamoDB 查询