使用 Xpath 无法找到 table 中的行数

Unable to find number of rows in table using Xpath

当我试图在第一个 table 中查找 tr(class-tr-heading) 的数量时,如果我使用 Xpath(与CSS 选择器-returns 1 tr)

<div class="large 20 columns">
  <table class="Red"><tbody>
    <tr class="tr-heading"></tr>
    <tr></tr>
  </tbody></table>
  <table class="Red"><tbody>
    <tr class="tr-heading"></tr>
    <tr></tr>
  </tbody></table>
</div>

下面一个return两个tr,

    WebElement tbl=dr.findElement(By.xpath("//div[@class='large 20 columns']/table[1]/tbody"));
    List<WebElement> trs=tbl.findElements(By.xpath("//tr[@class='tr-heading']"));
    System.out.println(trs.size());

下面的代码 returns s single tr,

WebElement tbl=dr.findElement(By.xpath("//div[@class='large 20 columns']/table[1]/tbody"));
List<WebElement> trs1=tbl.findElements(By.cssSelector("tr[class='tr-heading']"));
System.out.println(trs1.size());

似乎 Xpath 占用了整个 div 和 return 两个 tr.Can 请告诉我为什么会有这种差异以及我必须如何使用 xpath。

使用点(.)选择当前节点

    WebElement tbl=dr.findElement(By.xpath("//div[@class='large 20 columns']/table[1]/tbody"));
    List<WebElement> trs=tbl.findElements(By.xpath(".//tr[@class='tr-heading']"));
     System.out.println(trs.size());
   //Now it will print out 1 as in the current node there is only one tr tagwith the specified class name