Итерация по html-таблице с помощью Selenium / Katalon - PullRequest
1 голос
/ 06 марта 2019

Я пытаюсь перебрать таблицу 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

Вы можете мне помочь?

Еще один вопрос: как мне дождаться загрузки страницы после того, как я выбрал фильтр «Котировки»?

Ответы [ 2 ]

0 голосов
/ 06 марта 2019

Вы можете непосредственно найти все ячейки таблицы в первом столбце, где находится 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());
0 голосов
/ 06 марта 2019

попробуйте этот xpath. Он вернет все ячейки, содержащие значение SO

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

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