Reputation: 5472
Trying to get all td
(cells) under a specific th
(header)
HTML:
<table width="800">
<tbody>
<tr>
<th><b>User ID</b></th>
<th><b>Workforce ID</b></th>
<th><b>Given Name</b></th>
<th><b>Surname</b></th>
<th><b>Division</b></th>
<th><b>Hire Date</b></th>
<th><b>Department</b></th>
<th><b>emplType</b></th>
<th><b>Associations</b></th>
<th><b>SAP Roles</b></th>
<th><b>Community</b></th>
<th><b>Active</b></th>
</tr>
<tr>
<td class="active"><a href="?username=utest001">utest001</a></td>
<td class="active">00700001</td>
<td class="active">User</td>
<td class="active">Test001</td>
<td class="active">Coles</td>
<td class="active">2015-10-27</td>
<td class="active">CHFITVB</td>
<td class="active">S</td>
<td class="active">IAM</td>
<td class="active"></td>
<td class="active">Workforce</td>
<td class="active">Y</td>
</tr>
<tr>
<td class="disabled"><a href="?username=utest002">utest002</a></td>
<td class="disabled">00700002</td>
<td class="disabled">Vasia</td>
<td class="disabled">Smith</td>
<td class="disabled">Coles</td>
<td class="disabled">2015-11-16</td>
<td class="disabled">C0584SV</td>
<td class="disabled">W</td>
<td class="disabled"></td>
<td class="disabled"></td>
<td class="disabled">Workforce</td>
<td class="disabled">N</td>
</tr>
</tbody>
</table>
Method:
public List<WebElement> getCellsUnderHeader(WebElement table, String headerName) {
List<WebElement> thList = table.findElements(By.cssSelector("th > b"));
List<String> headers = new ArrayList<>();
thList.stream().forEach(th -> headers.add(th.getText()));
int index = headers.indexOf(headerName);
List<WebElement> trList = table.findElements(By.tagName("tr"));
List<WebElement> tdList = new ArrayList<>();
List<WebElement> columnCells = new ArrayList<>();
WebElement cell = null;
for (WebElement tr : trList) {
tdList = tr.findElements(By.tagName("td"));
cell = tdList.get(index); // Getting out of bounds here
columnCells.add(cell);
}
return columnCells;
}
Actual Result:
java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
Expected Result:
Return a List<WebElement>
containing 2 td
(with a
tag) under header User ID
.
Upvotes: 1
Views: 1579
Reputation: 50819
trList
contains the <tr>
with the headers, which doesn't have <td>
tags so in the first for
iteration tdList
is empty and tdList.get(index)
throws IndexOutOfBoundsException
.
You can iterate by index instead
for (int i = 1 ; i < trList.size() ; i++) {
tdList = trList.get(i).findElements(By.tagName("td"));
cell = tdList.get(index);
columnCells.add(cell);
}
Or ship first item
for (WebElement tr : trList.subList(1, trList.size())) {
tdList = tr.findElements(By.tagName("td"));
cell = tdList.get(index);
columnCells.add(cell);
}
Upvotes: 2
Reputation: 2760
Problem :
For the first tr iteration there was no td available, which in result causing the exception
Use Following :
public List<WebElement> getCellsUnderHeader(WebElement table, String headerName) {
List<WebElement> thList = table.findElements(By.cssSelector("th > b"));
List<String> headers = new ArrayList<>();
thList.stream().forEach(th -> headers.add(th.getText()));
int index = headers.indexOf(headerName);
System.out.println("Index : "+index);
List<WebElement> trList = table.findElements(By.tagName("tr"));
List<WebElement> tdList = new ArrayList<>();
List<WebElement> columnCells = new ArrayList<>();
WebElement cell = null;
for (WebElement tr : trList) {
tdList = tr.findElements(By.tagName("td"));
if (tdList.size()>0){
cell = tdList.get(index); // Getting out of bounds here
columnCells.add(cell);
}
}
return columnCells;
}
Upvotes: 2
Reputation: 2545
List<String> userIDs = driver.findElements(By.xpath("//td/a"))
.stream()
.map(elem -> elem.getText())
.collect(Collectors.toList());
Upvotes: 0