首页 > 解决方案 > 使用 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>

标签: javaxpathxpath-2.0

解决方案


代替

count(//div[@class='rating-container'][1]//span[@class='show-for-sr'])

用这个

count((//div[@class='rating-container'])[1]//span[@class='show-for-sr'])

您应该将整个 div 包装在索引中。


推荐阅读