【问题标题】:How to get all <tr> elements from table using Selenium如何使用 Selenium 从表中获取所有 <tr> 元素
【发布时间】:2017-01-01 18:11:34
【问题描述】:

我不知道为什么我只得到一个&lt;tr&gt; 元素。我搜索我的桌子

WebElement tableProducts = driver.findElement(By.id("gridAvailableProducts"));

然后我想找到所有&lt;tr&gt; 元素

List<WebElement> tableRows = tableProducts.findElements(By.tagName("tr"));

但是当我检查 tableRows 大小时,我得到 1。我没有得到它,因为我之前使用了这个示例代码,它工作正常。 这是我的 HTML:

<table id="gridAvailableProducts">
<thead>
<tr id="gridProductsHead">
    <th class="gridImgHeader"></th>
    <th class="gridNameHeader"><a href="#" id="name" class="sortAvailableProducts sortArrowAsc">Product name</a></th>
    <th class="gridIndexHeader"><a href="#" id="productindex" class="sortAvailableProducts">Index</a></th>
    <th class="gridProductPackHeader"><a href="#" id="IDDictUnitOfMeasure" class="sortAvailableProducts">Product pack</a></th>
    <th class="gridNetPriceHeader"><a href="#" id="netprice" class="sortAvailableProducts">Net price</a></th>
    <th></th>
</tr>
</thead>
<tbody>
<tr class="addProductToyaTr" data-frequency="0/0/0" data-weight="0" data-volume="0" data-account="0" data-mccount="0" data-ibcount="125" data-smallestpackquantity="125" data-unit="PCE" data-pkwiu="" data-price="3.43" data-id="10731" data-index="INDEX_10731">
    <td class="pImg"><a title="Produkt 10731" style="z-index:1000; position:relative;" rel="grp1" class="bigImg" href="/B2B/img/INDEX_10731.jpg">
        <img alt="" width="45" height="45" src="/B2B/images/nofoto.jpg" onerror="this.onerror=null;this.src='/B2B/images/nofoto.jpg'"></a></td>
    <td class="pName gridProductNameRow">Produkt 10731</td>
    <td class="gridProductIndexRow">INDEX_10731</td>
    <td class="gridProductPackRow">PCE</td>
    <td class="gridProductPriceRow">3.43</td>
    <td><div class="addProductToyaGreen"></div></td>
</tr>
<tr class="addProductToyaTr" data-frequency="0/0/0" data-weight="0" data-volume="0" data-account="0" data-mccount="0" data-ibcount="125" data-smallestpackquantity="125" data-unit="PCE" data-pkwiu="" data-price="10.36" data-id="10732" data-index="INDEX_10732">
    <td class="pImg"><a title="Produkt 10732" style="z-index:1000; position:relative;" rel="grp1" class="bigImg" href="/B2B/img/INDEX_10732.jpg">
        <img alt="" width="45" height="45" src="/B2B/images/nofoto.jpg" onerror="this.onerror=null;this.src='/B2B/images/nofoto.jpg'"></a></td>
    <td class="pName gridProductNameRow">Produkt 10732</td><td class="gridProductIndexRow">INDEX_10732</td>
    <td class="gridProductPackRow">PCE</td><td class="gridProductPriceRow">10.36</td>
    <td><div class="addProductToyaGreen"></div></td>
</tr>
<tr class="addProductToyaTr" data-frequency="0/0/0" data-weight="0" data-volume="0" data-account="0" data-mccount="0" data-ibcount="125" data-smallestpackquantity="125" data-unit="PCE" data-pkwiu="" data-price="8.18" data-id="10733" data-index="INDEX_10733">
    <td class="pImg"><a title="Produkt 10733" style="z-index:1000; position:relative;" rel="grp1" class="bigImg" href="/B2B/img/INDEX_10733.jpg">
        <img alt="" width="45" height="45" src="/B2B/images/nofoto.jpg" onerror="this.onerror=null;this.src='/B2B/images/nofoto.jpg'"></a></td>
    <td class="pName gridProductNameRow">Produkt 10733</td><td class="gridProductIndexRow">INDEX_10733</td>
    <td class="gridProductPackRow">PCE</td><td class="gridProductPriceRow">8.18</td>
    <td><div class="addProductToyaGreen"></div></td>
</tr>
<tr class="addProductToyaTr" data-frequency="0/0/0" data-weight="0" data-volume="0" data-account="0" data-mccount="0" data-ibcount="125" data-smallestpackquantity="125" data-unit="PCE" data-pkwiu="" data-price="10.4" data-id="10734" data-index="INDEX_10734">
    <td class="pImg"><a title="Produkt 10734" style="z-index:1000; position:relative;" rel="grp1" class="bigImg" href="/B2B/img/INDEX_10734.jpg">
        <img alt="" width="45" height="45" src="/B2B/images/nofoto.jpg" onerror="this.onerror=null;this.src='/B2B/images/nofoto.jpg'"></a></td>
    <td class="pName gridProductNameRow">Produkt 10734</td>
    <td class="gridProductIndexRow">INDEX_10734</td>
    <td class="gridProductPackRow">PCE</td>
    <td class="gridProductPriceRow">10.40</td>
    <td><div class="addProductToyaGreen"></div></td>
</tr>
<tr class="addProductToyaTr" data-frequency="0/0/0" data-weight="0" data-volume="0" data-account="0" data-mccount="0" data-ibcount="125" data-smallestpackquantity="125" data-unit="PCE" data-pkwiu="" data-price="13.66" data-id="10735" data-index="INDEX_10735">
    <td class="pImg"><a title="Produkt 10735" style="z-index:1000; position:relative;" rel="grp1" class="bigImg" href="/B2B/img/INDEX_10735.jpg">
        <img alt="" width="45" height="45" src="/B2B/images/nofoto.jpg" onerror="this.onerror=null;this.src='/B2B/images/nofoto.jpg'"></a></td>
    <td class="pName gridProductNameRow">Produkt 10735</td><td class="gridProductIndexRow">INDEX_10735</td>
    <td class="gridProductPackRow">PCE</td>
    <td class="gridProductPriceRow">13.66</td>
    <td><div class="addProductToyaGreen"></div></td>
</tr>
</tbody>

当我尝试通过xpath 获取元素时

WebElement element = driver.findElement(By.xpath("//*[@id=\"gridAvailableProducts\"]/tbody/tr[3]"));

我收到错误提示

org.openqa.selenium.NoSuchElementException: 没有这样的元素

【问题讨论】:

  • 你的代码看起来是正确的,你可以在调试失败时截图。
  • 此代码运行时表格是否已完全填充?也许您需要等待它显示。
  • @BreaksSoftware 你是对的。对于测试,我将线程睡眠 10 秒,然后我得到了很好的尺寸 :)

标签: java html selenium


【解决方案1】:

使用WebDriverWaitExpectedConditions.visibilityOfNestedElementsLocatedBy 的最佳方法不是睡眠线程,而是等待嵌套元素变为可见,如下所示:-

WebElement tableProducts = driver.findElement(By.id("gridAvailableProducts"));

List<WebElement> tableRows = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfNestedElementsLocatedBy(tableProducts, By.tagName("tr")));

【讨论】:

  • 谢谢,我知道,但是为了快速检查,我使用了 Thread.sleep() 然后我转到了 WebDriverWait。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-23
  • 2022-01-11
  • 2013-05-22
  • 2018-11-09
  • 2020-12-11
  • 2021-08-27
相关资源
最近更新 更多