Question

I have a datagrid that I need to retrieve the value next to the item that I can identify dynamically.

What the scenario is trying to do: This is HTML form a Roles Datagrid which contains a Role Name and a count of permissions. An admin would click on a role, a dialog box would pop up and the admin would update the role and save. The Role count would update. My script checks that the number increments and decrements based on the scenario.

What I'm trying to do is come up with a script that will search based on role name in the "data-rk" attribute and then collect the data in the adjacent permissions count column.

Here is the table:

<tbody id="rolesForm:rolesTable_data" class="ui-datatable-data ui-widget-content">
      <tr class="ui-widget-content ui-datatable-even" aria-selected="false" role="row"            
          data-rk="Role1" data-ri="0">
          <td role="gridcell">Role1</td>
          <td role="gridcell">
             <span id="rolesForm:rolesTable:0:permissions">3</span>
          </td>
       </tr>
       <tr class="ui-widget-content ui-datatable-odd ui-state-hover" aria-
           selected="false" role="row" data-rk="Role2" data-ri="1">
          <td role="gridcell">Role2</td>
          <td role="gridcell">
              <span id="rolesForm:rolesTable:1:permissions">38</span>
          </td>
       </tr>

Any suggestion on how to retrieve that data?

Was it helpful?

Solution

The following code will help you get the count for the specific data-rk attribute that you want. For this example, I am searching for a tr element having data-rk attribute "Role1"

First find the table body element by it's ID attribute and then get the string in the permissions count column from within this element as follows:

    WebElement table = driver.findElementById("rolesForm:rolesTable_data");
    String pCount = table.findElement(By.xpath("/tr[@data-rk=\"Role1\"]/td[2]")).getText();
    // The returned object is a string. If you like, you could convert it to an integer
    int count = Integer.parseInt(pCount);

Please note that this code will only return the first tr element that satisfies the condition. If your table is going to have multiple tr elements that satisfy this condition, then you will have to create a list of tr elements and then iterate over them to get the count. You can get the list as follows:

    WebElement table = driver.findElementById("rolesForm:rolesTable_data");
    List<WebElement> rows = table.findElements(By.xpath("/tr[@data-rk=\"Role1\"]);
    // Iterate over rows to get each count
    for (WebElement row : rows) {
        String pCount = row.findElement(By.xpath("/td[2]")).getText();
        // Do whatever you want with pCount
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top