0

I have a tbody with multiple trs and 10 tds per tr.

I want to iterate through the trs and get the .text() of only specific tds. Below is the section where I itterate throught the trs which have been assigned to a List (alertsTableRowsList)

for (int i = 0; i < alertsTableRowsCount; i++){     

            String reference = alertsTableRowsList.get(i).findElement(By.xpath("//td[@data-title-text = 'Reference']")).getText(); //td number 3
            String classification = alertsTableRowsList.get(i).findElement(By.xpath("//td[@data-title-text = 'Classification']")).getText(); //td number 4
            String description = alertsTableRowsList.get(i).findElement(By.xpath("//td[@data-title-text = 'Description']")).getText(); //td number 7
            String status = alertsTableRowsList.get(i).findElement(By.xpath("//td[@data-title-text = 'Status']")).getText(); //td number 8
            String date = alertsTableRowsList.get(i).findElement(By.xpath("//td[@data-title-text = 'Date']")).getText(); //td number 9
            String owner = alertsTableRowsList.get(i).findElement(By.xpath("//td[@data-title-text = 'Owner']")).getText(); //td number 10

            System.out.println("reference : " + reference);
            System.out.println("classification : " + classification);
            System.out.println("description : " + description);
            System.out.println("status : " + status);
            System.out.println("date : " + date);
            System.out.println("owner : " + owner); 

        }

What I notice is that only the .text() of the tds in the first tr is returned. In this case it is returned twice because in this case I have only two trs in my tbody.

Output:

reference : AML_AC.20161102.12
classification : AML Central Alert
Wholesale - Wire transfer
description : Thoughtbeat Ltd
status : Analysis
date : 11/02/2016
owner : Strydom, AJ (Rico)

reference : AML_AC.20161102.12
classification : AML Central Alert
Wholesale - Wire transfer
description : Thoughtbeat Ltd
status : Analysis
date : 11/02/2016
owner : Strydom, AJ (Rico)

HTML for the tbody:

<tbody>
   <!-- ngRepeat: searchResult in $data -->
   <tr ng-repeat="searchResult in $data" ng-click="vm.clickEntity(searchResult)" ng-dblclick="vm.dblClickEntity()" ng-class="{ 'is-selected': vm.isEntitySelected(searchResult.id) }" class="pointer ng-scope" title="Thoughtbeat Ltd">
      <!-- ngIf: core.user.can('webhq.home.assign') -->
      <td data-title="''" class="table-icon-cell ng-scope" ng-if="core.user.can('webhq.home.assign')" ng-click="$event.stopPropagation()" header="vm.checkboxTemplate" data-title-text="">
         <!-- ngIf: vm.isEditable(searchResult) -->
         <div class="checkbox ng-scope" ng-if="vm.isEditable(searchResult)"><label><input type="checkbox" ng-model="vm.checkedEntities[searchResult.id]" ng-change="vm.entityChecked()" class="ng-pristine ng-untouched ng-valid ng-empty"> <span class="checkbox-label"></span></label></div>
         <!-- end ngIf: vm.isEditable(searchResult) -->
      </td>
      <!-- end ngIf: core.user.can('webhq.home.assign') -->
      <td data-title="'*'" sortable="'attentionIcon'" class="table-icon-cell" data-title-text="*"><img bf-image-path="" class="table-icon" src="/Assets/img/icons/_blank.png"></td>
      <td data-title="'HOME.REFERENCE' | translate" sortable="'reference'" data-title-text="Reference" class="ng-binding">AML_AC.20161102.12</td>
      <td data-title="'HOME.CLASSIFICATION' | translate" sortable="'classification.id'" data-title-text="Classification" class="ng-binding">
         <img bf-image-path="writtentransfer" class="table-icon" src="/Assets/img/icons/writtentransfer.png"> AML Central Alert <!-- ngIf: ::searchResult.variation.displayName --><small class="text-muted ng-binding ng-scope" ng-if="::searchResult.variation.displayName"><br><img bf-image-path="'_blank'" class="table-icon" src="/Assets/img/icons/_blank.png"> Wholesale - Wire transfer</small><!-- end ngIf: ::searchResult.variation.displayName -->
      </td>
      <td data-title="'*'" sortable="'attachmentCount'" class="table-icon-cell" data-title-text="*"><img bf-image-path="_blank" class="table-icon" title="0 files attached" src="/Assets/img/icons/_blank.png"></td>
      <td data-title="'*'" sortable="'notesCount'" class="table-icon-cell" data-title-text="*"><img bf-image-path="_blank" class="table-icon" title="0 notes" src="/Assets/img/icons/_blank.png"></td>
      <td data-title="'HOME.NAME' | translate" sortable="'displayName'" data-title-text="Description"><img bf-image-path="priority_normal" class="table-icon" src="/Assets/img/icons/priority_normal.png"> <strong class="ng-binding">Thoughtbeat Ltd</strong></td>
      <td data-title="'HOME.STATUS' | translate" sortable="'status.progress'" data-title-text="Status"><img bf-image-path="microscope" class="table-icon" src="/Assets/img/icons/microscope.png"> <span ng-class="{ 'text-danger': searchResult.status.attract }" class="ng-binding">Analysis</span></td>
      <td data-title="'HOME.DATE' | translate" sortable="'startDate'" data-title-text="Date"><span ng-hide="searchResult.isDue" class="ng-binding">11/02/2016</span> <span ng-show="searchResult.isDue" class="text-danger ng-hide"><strong class="ng-binding">due in 2 days</strong></span></td>
      <td data-title="'HOME.OWNER' | translate" sortable="'ownerName'" data-title-text="Owner" class="ng-binding">
         Strydom, AJ (Rico) <!-- ngIf: ::searchResult.teamName --><small ng-if="::searchResult.teamName" class="text-muted ng-binding ng-scope"><br>TEST role</small><!-- end ngIf: ::searchResult.teamName -->
      </td>
   </tr>
   <!-- end ngRepeat: searchResult in $data -->
   <tr ng-repeat="searchResult in $data" ng-click="vm.clickEntity(searchResult)" ng-dblclick="vm.dblClickEntity()" ng-class="{ 'is-selected': vm.isEntitySelected(searchResult.id) }" class="pointer ng-scope" title="Skiba Ltd">
      <!-- ngIf: core.user.can('webhq.home.assign') -->
      <td data-title="''" class="table-icon-cell ng-scope" ng-if="core.user.can('webhq.home.assign')" ng-click="$event.stopPropagation()" header="vm.checkboxTemplate" data-title-text="">
         <!-- ngIf: vm.isEditable(searchResult) -->
         <div class="checkbox ng-scope" ng-if="vm.isEditable(searchResult)"><label><input type="checkbox" ng-model="vm.checkedEntities[searchResult.id]" ng-change="vm.entityChecked()" class="ng-pristine ng-untouched ng-valid ng-empty"> <span class="checkbox-label"></span></label></div>
         <!-- end ngIf: vm.isEditable(searchResult) -->
      </td>
      <!-- end ngIf: core.user.can('webhq.home.assign') -->
      <td data-title="'*'" sortable="'attentionIcon'" class="table-icon-cell" data-title-text="*"><img bf-image-path="" class="table-icon" src="/Assets/img/icons/_blank.png"></td>
      <td data-title="'HOME.REFERENCE' | translate" sortable="'reference'" data-title-text="Reference" class="ng-binding">AML_AC.20161102.9</td>
      <td data-title="'HOME.CLASSIFICATION' | translate" sortable="'classification.id'" data-title-text="Classification" class="ng-binding">
         <img bf-image-path="writtentransfer" class="table-icon" src="/Assets/img/icons/writtentransfer.png"> AML Central Alert <!-- ngIf: ::searchResult.variation.displayName --><small class="text-muted ng-binding ng-scope" ng-if="::searchResult.variation.displayName"><br><img bf-image-path="'_blank'" class="table-icon" src="/Assets/img/icons/_blank.png"> Private Banking - Wire Transfer</small><!-- end ngIf: ::searchResult.variation.displayName -->
      </td>
      <td data-title="'*'" sortable="'attachmentCount'" class="table-icon-cell" data-title-text="*"><img bf-image-path="_blank" class="table-icon" title="0 files attached" src="/Assets/img/icons/_blank.png"></td>
      <td data-title="'*'" sortable="'notesCount'" class="table-icon-cell" data-title-text="*"><img bf-image-path="_blank" class="table-icon" title="0 notes" src="/Assets/img/icons/_blank.png"></td>
      <td data-title="'HOME.NAME' | translate" sortable="'displayName'" data-title-text="Description"><img bf-image-path="priority_normal" class="table-icon" src="/Assets/img/icons/priority_normal.png"> <strong class="ng-binding">Skiba Ltd</strong></td>
      <td data-title="'HOME.STATUS' | translate" sortable="'status.progress'" data-title-text="Status"><img bf-image-path="microscope" class="table-icon" src="/Assets/img/icons/microscope.png"> <span ng-class="{ 'text-danger': searchResult.status.attract }" class="ng-binding">Analysis</span></td>
      <td data-title="'HOME.DATE' | translate" sortable="'startDate'" data-title-text="Date"><span ng-hide="searchResult.isDue" class="ng-binding">11/02/2016</span> <span ng-show="searchResult.isDue" class="text-danger ng-hide"><strong class="ng-binding">due in 3 days</strong></span></td>
      <td data-title="'HOME.OWNER' | translate" sortable="'ownerName'" data-title-text="Owner" class="ng-binding">
         Strydom, AJ (Rico) <!-- ngIf: ::searchResult.teamName --><small ng-if="::searchResult.teamName" class="text-muted ng-binding ng-scope"><br>TEST role</small><!-- end ngIf: ::searchResult.teamName -->
      </td>
   </tr>
   <!-- end ngRepeat: searchResult in $data -->
</tbody>
1
  • You should create your custom component. One of the selenium TestNG framework QMetry Automation Framework provides a way for such requirement.Please refer how to create custom component Commented Nov 10, 2016 at 11:01

1 Answer 1

2

If alertsTableRowsList is your List of WebElements then you could iterate like this:

 for (int i = 0; i < alertsTableRowsList.size(); i++){     
    String reference = driver.findElement(By.xpath("//tbody/tr[" + (i + 1) + "]/td[@data-title-text = 'Reference']")).getText();
    String classification = driver.findElement(By.xpath("//tbody/tr[" + (i + 1) + "]/td[@data-title-text = 'Classification']")).getText(); //td number 4
    String description = driver.findElement(By.xpath("//tbody/tr[" + (i + 1) + "]/td[@data-title-text = 'Description']")).getText(); //td number 7
    String status = driver.findElement(By.xpath("//tbody/tr[" + (i + 1) + "]/td[@data-title-text = 'Status']")).getText(); //td number 8
    String date = driver.findElement(By.xpath("//tbody/tr[" + (i + 1) + "]/td[@data-title-text = 'Date']")).getText(); //td number 9
    String owner = driver.findElement(By.xpath("//tbody/tr[" + (i + 1) + "]/td[@data-title-text = 'Owner']")).getText(); //td number 10

    System.out.println("reference : " + reference);
    System.out.println("classification : " + classification);
    System.out.println("description : " + description);
    System.out.println("status : " + status);
    System.out.println("date : " + date);
    System.out.println("owner : " + owner); 
}
Sign up to request clarification or add additional context in comments.

5 Comments

Also with your suggestion the content of the first row in my list are repeated twice. (as in my case). What could also say is that the alertsTableRowsList consist out of another list and that is the alertsTableColumnsList which in turn will have all the WebElements (td).
Provide your HTML portion.
html added to original post
see the edited answer. you may need to change the code portion for getting list of rows (alertsTableRowsList). you may replace the css selector with xpath too.
How is that different from : List<WebElement> alertsTableRowsList = alertsTable.findElements(By.tagName("tr"));

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.