使用 Selenium/Katalon 在 html 表上进行迭代

    我正在尝试遍历 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>


小怪兽爱吃肉
浏览 79回答 2
2回答

侃侃尔雅

您可以直接在内部的第一列中找到所有表格单元格SOxxx。List<WebElement> first_cloumns = driver.findElements(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;By.cssSelector("div.o_sale_order tbody td:nth-child(1)"));List<String> values = first_cloumns&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .stream() // require JDK 8 and higher&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(td -> td.getText())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());

当年话下

试试这个 xpath。它将返回所有包含 SO 值的单元格By.xpath("//table//td[contains(text(),'SO')]");List<WebElement> rows = driver.findElements(By.xpath("//table//td[contains(text(),'SO')]"));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java