How can I count and select some rows in a Web table using Selenium?

假装没事ソ 提交于 2020-04-30 09:08:26

问题


I'm developing an Automation test using Selenium WebDriver and Java, I need to assured that there are items in the web table, and select one of those items, but the ID is dynamic.

HTML code:

<table class="datagrid-btable" cellspacing="0" cellpadding="0" border="0" style="table-layout: auto;">    <tbody>
      <tr id="datagrid-row-r4-2-0" datagrid-row-index="0" class="datagrid-row datagrid-row-selected">
         <td field="PLANT_CODE" style="display:none;">
            <div style="text-align: left;" class="datagrid-cell datagrid-cell-c4-PLANT_CODE">1001</div>
         </td>
         <td field="PLANT_NM">
            <div style=";text-align:center;" class="datagrid-cell datagrid-cell-c4-PLANT_NM">TESTE1</div>
         </td>
         <td field="PU_NAME" style="display:none;">
            <div style=";text-align:left;" class="datagrid-cell datagrid-cell-c4-PU_NAME"></div>
         </td>
         <td field="SUPPLIER_CODE">
            <div style=";text-align:center;" class="datagrid-cell datagrid-cell-c4-SUPPLIER_CODE">SUP001AR</div>
         </td>
         <td field="SUPPLIER_NM">
            <div style=";text-align:center;" class="datagrid-cell datagrid-cell-c4-SUPPLIER_NM">SUPPLIER 001 AR</div>
         </td>
         <td field="ITEM_CODE">
            <div style=";text-align:center;" class="datagrid-cell datagrid-cell-c4-ITEM_CODE">ITEM001AR</div>
         </td>
         <td field="ITEM_NM">
            <div style=";text-align:left;" class="datagrid-cell datagrid-cell-c4-ITEM_NM">ITEM1 AR</div>
         </td>
         <td field="WRHOUSNG_NO" style="display:none;">
            <div style=";text-align:center;" class="datagrid-cell datagrid-cell-c4-WRHOUSNG_NO"></div>
         </td>
         <td field="ORDE_NO" style="display:none;">
            <div style=";text-align:center;" class="datagrid-cell datagrid-cell-c4-ORDE_NO"></div>
         </td>
      </tr>
      <tr id="datagrid-row-r4-2-1" datagrid-row-index="1" class="datagrid-row">
         <td field="PLANT_CODE" style="display:none;">
            <div style="text-align: left;" class="datagrid-cell datagrid-cell-c4-PLANT_CODE">1001</div>
         </td>
         <td field="PLANT_NM">
            <div style=";text-align:center;" class="datagrid-cell datagrid-cell-c4-PLANT_NM">BOCAR LERMA</div>
         </td>
         <td field="PU_NAME" style="display:none;">
            <div style=";text-align:left;" class="datagrid-cell datagrid-cell-c4-PU_NAME"></div>
         </td>
         <td field="SUPPLIER_CODE">
            <div style=";text-align:center;" class="datagrid-cell datagrid-cell-c4-SUPPLIER_CODE">SUP001AR</div>
         </td>
         <td field="SUPPLIER_NM">
            <div style=";text-align:center;" class="datagrid-cell datagrid-cell-c4-SUPPLIER_NM">SUPPLIER 001 AR</div>
         </td>
         <td field="ITEM_CODE">
            <div style=";text-align:center;" class="datagrid-cell datagrid-cell-c4-ITEM_CODE">ITEM001AR</div>
         </td>
         <td field="ITEM_NM">
            <div style=";text-align:left;" class="datagrid-cell datagrid-cell-c4-ITEM_NM">ITEM1 AR</div>
         </td>
         <td field="WRHOUSNG_NO" style="display:none;">
            <div style=";text-align:center;" class="datagrid-cell datagrid-cell-c4-WRHOUSNG_NO">PUR1</div>
         </td>
         <td field="ORDE_NO" style="display:none;">
            <div style=";text-align:center;" class="datagrid-cell datagrid-cell-c4-ORDE_NO">PUR1</div>
         </td>
      </tr>    </tbody> </table>

I tried "My code":

WebElement tbody = driver.findElement(By.xpath("//*[@class='datagrid-body']/tbody/tr"));

In this case, my request is returning two rows so how can I count the rows and select one of these? Thanks


回答1:


Try this step for get you want.

1. Table initialize

In your case, the table has the class datagrid-btable, the way to initialize it :

WebElement tbl = driver.findElement(By.className("datagrid-btable"));

2. Row initialize

The name tag for the web table row in general is tr, the way to initialize it :

List<WebElement> rows = tbl.findElements(By.tagName("tr"));

You can get rowCount with :

int count = rows.size();
System.out.println("count rows :" +count);

3. Column initialize

The name tag for the web table column in general are th or td, the way to initialize it :

td tag

List<WebElement> cols = rows.get(rowIndex).findElements(By.tagName("td"));

So you can select particular cell by :

String cell = cols.get(indexCol).getText();

Example for select col1 in row1 :

List<WebElement> cols = rows.get(0).findElements(By.tagName("td"));
String cell = cols.get(0).getText();
System.out.println("cell value :" +cell);

Or try this iteration for select all cell table :

for(int i=0; i<rows.size(); i++) {
    //check column each in row, identification with 'td' tag
    List<WebElement> cols = rows.get(i).findElements(By.tagName("td"));

    //column iteration
    for(int j=0; j<cols.size(); j++) {
        System.out.println("row " +(i+1) +" col " +(j+1) +" : " +cols.get(j).getText());
    }
}



回答2:


You can get count of the rows using findElements method:

List<WebElement> rows = driver.findElements(By.cssSelector(".datagrid-btable tr.datagrid-row"));
System.out.println(rows.size());

Easy way to select row with specific text using xpath:

// get row contains text 1001
driver.findElement(By.xpath("//table[@class='datagrid-btable']//tr[@class='datagrid-row' and contains(.,'1001')]")).click();

// get row by PLANT_CODE and exact cell value
driver.findElement(By.xpath("//table[@class='datagrid-btable']//tr[@class='datagrid-row' and .//td[@field='PLANT_CODE' and div[text()='1001']]]")).click();

To find row you need, you can use stream().filter(). In the example below, rows filtered by PLANT_NM text. Using filter or rows iteration, you can write a method to filter rows by any/multi cells:

List<WebElement> rows = driver.findElements(By.cssSelector(".datagrid-btable tr.datagrid-row"));
System.out.println(rows.size());

List<WebElement> filteredRows = rows.stream().filter(element ->
        element.findElement(By.cssSelector("td[field='PLANT_NM'")).getText().equals("BOCAR LERMA"))
        .collect(Collectors.toList());

Assert.assertTrue(filteredRows.size() > 0, "Row with \"BOCAR LERMA\" PLANT_NM exist.");
filteredRows.get(0).click();


来源:https://stackoverflow.com/questions/57467583/how-can-i-count-and-select-some-rows-in-a-web-table-using-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!