使用Selenium / Katalon迭代html表

问题描述 投票:1回答:2

我正在尝试遍历HTML表格以从中获取值,但我没有得到所需的结果。这是HTML代码:

<div class="o_sale_order table-responsive">
  <table class="o_list_view table table-sm table-hover table-striped o_list_view_ungrouped">
    <thead>
      <tr>
        <th width="1" class="o_list_record_selector">
          <div class="custom-control custom-checkbox">
            <input type="checkbox" id="checkbox-868" class="custom-control-input">
            <label for="checkbox-868" class="custom-control-label"></label></div>
        </th>
        <th class="o_column_sortable" data-original-title="" title="">Quotation Number</th>
        <th class="o_column_sortable" data-original-title="" title="">Quotation Date</th>
        <th class="o_column_sortable" data-original-title="" title="">Customer</th>
        <th class="o_column_sortable" data-original-title="" title="">Salesperson</th>
        <th class="o_column_sortable" style="text-align: right;" data-original-title="" title="">Total</th>
        <th class="o_column_sortable" data-original-title="" title="">Status</th>
      </tr>
    </thead>
    <tbody class="ui-sortable">
      <tr class="o_data_row">
        <td width="1" class="o_list_record_selector">
          <div class="custom-control custom-checkbox">
            <input type="checkbox" id="checkbox-869" class="custom-control-input">
            <label for="checkbox-869" class="custom-control-label"></label></div>
        </td>
        <td class="o_data_cell o_readonly_modifier o_required_modifier">SO107</td>
        <td class="o_data_cell o_required_modifier">03/04/2019 17:40:46</td>
        <td class="o_data_cell o_required_modifier">AA</td>
        <td class="o_data_cell">Administrator</td>
        <td class="o_data_cell o_list_number o_monetary_cell o_readonly_modifier">
          <span class="o_field_monetary o_field_number o_field_widget o_readonly_modifier" name="amount_total">305.00&nbsp;€</span></td>
        <td class="o_data_cell o_readonly_modifier">Quotation Sent</td>
      </tr>
      <tr class="o_data_row">
        <td width="1" class="o_list_record_selector">
          <div class="custom-control custom-checkbox">
            <input type="checkbox" id="checkbox-870" class="custom-control-input">
            <label for="checkbox-870" class="custom-control-label">​</label></div>
        </td>
        <td class="o_data_cell o_required_modifier">SO055</td>
        <td class="o_data_cell o_required_modifier">03/01/2019 20:24:35</td>
        <td class="o_data_cell o_required_modifier">AA</td>
        <td class="o_data_cell">Administrator</td>
        <td class="o_data_cell o_list_number o_monetary_cell o_readonly_modifier">
          <span class="o_field_monetary o_field_number o_field_widget o_readonly_modifier" name="amount_total">2.44&nbsp;€</span></td>
        <td class="o_data_cell o_readonly_modifier">Quotation</td>
      </tr>
      <tr class="o_data_row">
        <td width="1" class="o_list_record_selector">
          <div class="custom-control custom-checkbox"><input type="checkbox" id="checkbox-871" class="custom-control-input">
            <label for="checkbox-871" class="custom-control-label">​</label></div>
        </td>
        <td class="o_data_cell o_required_modifier">SO039</td>
        <td class="o_data_cell o_required_modifier">03/01/2019 12:16:08</td>
        <td class="o_data_cell o_required_modifier">AA</td>
        <td class="o_data_cell">Administrator</td>
        <td class="o_data_cell o_list_number o_monetary_cell o_readonly_modifier">
          <span class="o_field_monetary o_field_number o_field_widget o_readonly_modifier" name="amount_total">2.44&nbsp;€</span></td>
        <td class="o_data_cell o_readonly_modifier">Quotation</td>
      </tr>
      <tr class="o_data_row">
        <td width="1" class="o_list_record_selector">
          <div class="custom-control custom-checkbox">
            <input type="checkbox" id="checkbox-872" class="custom-control-input">
            <label for="checkbox-872" class="custom-control-label">​</label></div>
        </td>
        <td class="o_data_cell o_required_modifier">SO025</td>
        <td class="o_data_cell o_required_modifier">02/28/2019 19:50:59</td>
        <td class="o_data_cell o_required_modifier">BB</td>
        <td class="o_data_cell">Administrator</td>
        <td class="o_data_cell o_list_number o_monetary_cell o_readonly_modifier">
          <span class="o_field_monetary o_field_number o_field_widget o_readonly_modifier" name="amount_total">2.44&nbsp;€</span></td>
        <td class="o_data_cell o_readonly_modifier">Quotation</td>
      </tr>
    </tbody>
    <tfoot>
      <tr>
        <td></td>
        <td class="name"></td>
        <td class="date_order"></td>
        <td class="partner_id"></td>
        <td class="user_id"></td>
        <td class="amount_total o_list_number" title="Total Tax Included">312.32</td>
        <td class="state"></td>
      </tr>
    </tfoot>
  </table>
</div>
</div>
</div>
</main>
</div>

我需要从每行获取以“SO”开头的值,如下所示:

<td class="o_data_cell o_required_modifier">SO055</td>

我尝试过:

List<String> list = new ArrayList<>();
        driver.findElement(By.xpath("//*[@class = 'o_dropdown_toggler_btn btn btn-secondary dropdown-toggle' and (contains(text(), 'Filters') or contains(., 'Filters'))]")).click();
        driver.findElement(By.xpath("(//a[contains(text(),'Quotations')])[2]")).click();
        driver.findElement(By.xpath("//*[@class = 'o_dropdown_toggler_btn btn btn-secondary dropdown-toggle' and (contains(text(), 'Filters') or contains(., 'Filters'))]")).click();

List<WebElement> rows = driver.findElements(By.xpath("//table[@class='o_list_view table table-sm table-hover table-striped o_list_view_ungrouped']//tr[not(th)]"));
        Iterator<WebElement> iter = rows.iterator();
        while(iter.hasNext()) {
            WebElement tr = iter.next();
            WebElement td = tr.findElement(By.xpath("./td[(@class='o_data_cell o_readonly_modifier o_required_modifier')]"));
            listaPreventiviFatturabili.add(td.getText());
        }

但它不起作用。

这是相关网页的屏幕截图:

你能帮助我吗?

另一个问题:在为“报价单”选择过滤器后,如何等待页面加载?

java loops selenium html-table katalon-studio
2个回答
0
投票

试试这个xpath。它将返回所有含有SO值的细胞

By.xpath("//table//td[contains(text(),'SO')]");

List<WebElement> rows = driver.findElements(By.xpath("//table//td[contains(text(),'SO')]"));

0
投票

您可以直接在SOxxx所在的第一列中找到所有表格单元格。

List<WebElement> first_cloumns = driver.findElements(
                     By.cssSelector("div.o_sale_order tbody td:nth-child(1)"));


List<String> values = first_cloumns
                            .stream() // require JDK 8 and higher
                            .map(td -> td.getText())
                            .collect(Collectors.toList());
© www.soinside.com 2019 - 2024. All rights reserved.