java - 使用 Selenium/Katalon 迭代 html 表

标签 java loops selenium html-table katalon-studio

我正在尝试迭代 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());
        }

但它不起作用。

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

Screen

你能帮我吗?

另一个问题:选择“引用”过滤器后如何等待页面加载?

最佳答案

试试这个 xpath。它将返回包含 SO 值的所有单元格

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

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

关于java - 使用 Selenium/Katalon 迭代 html 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55022951/

相关文章:

java - 对 4GB JVM 和 3GB 缓存进行适当的 JVM/GC 调优

javascript - 如何重复向元素添加类及其在每个起点重置的后代

javascript - Angularjs,使用 angular-recaptcha 进行端到端测试

JQuery - 迭代值的集合

maven - 使用 Jenkins 和 Maven 可以安全运行多少个 selenium 测试脚本

python - 如何通过selenium停止点击同一个按钮,而该按钮始终存在

java - 单元测试双向转换器

JAVA 无法在 main from 方法中访问我的数组

java - setAutoCreateRowSorter 对象

c - 死循环,取不到值