0

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

4
  • 1
    Hi Cleicy, what have you tried? Where are you getting stuck? Commented Aug 12, 2019 at 20:15
  • look at attribute datagrid-row-index="0" increment it gradually and in every row you have specific set of columns which seems static Commented Aug 12, 2019 at 20:18
  • Hi @mrfreester :) First of all I tried to select the first row using id/xpath to know if there is results in the table, but I discovered that ID is dynamic so I can't map the element this way. So I tried "My code", but is not working too Commented Aug 12, 2019 at 20:35
  • Thanks! Do you care which row you get? You could probably just do "(//*[@class='datagrid-body']/tbody/tr)[1]" if you want to grab the 1st one. Or use findElements instead and grab the first one from the list. If you want to get a specific one the answer would be more complicated. Commented Aug 12, 2019 at 20:53

2 Answers 2

2

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());
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@CleicyGuião Welcome
0

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 :

// 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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.