gpt4 book ai didi

java - Selenium Webdriver 迭代行并仅检索某些列

转载 作者:行者123 更新时间:2023-12-01 09:16:15 30 4
gpt4 key购买 nike

我有一个 tbody,其中包含多个 tr,每个 tr 有 10 个 td

我想迭代 tr 并仅获取特定 td.text()。下面是我遍历已分配给 List (alertsTableRowsList)

tr 的部分
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);

}

我注意到,仅返回第一个 trtd.text()。在这种情况下,它会返回两次,因为在这种情况下,我的 tbody 中只有两个 tr

输出:

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:

<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>

最佳答案

如果alertsTableRowsList是您的WebElements列表,那么您可以像这样迭代:

 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);
}

关于java - Selenium Webdriver 迭代行并仅检索某些列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40524179/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com