user340977 user340977 - 15 days ago 9
Java Question

List<WebElement> for only first 3 columns of the table

Where it says

findElements(By.xpath("td[]"))
, how to get only the
td[1]
,
td[2]
,
td[3]
elements in the
List
?

WebElement Webtable = driver.findElement(By.xpath(""));
List<WebElement> TotalRowCount = Webtable.findElements(By.xpath(""));
int RowIndex = 1;
for (WebElement rowElement : TotalRowCount) {
List<WebElement> TotalColumnCount = rowElement.findElements(By.xpath("td[]"));
int ColumnIndex = 1;
for (WebElement colElement : TotalColumnCount) {
ColumnIndex = ColumnIndex +1;
}
RowIndex = RowIndex + 1;
}

Answer

You should try using position() function of the xpath as below which would select only first three columns for each row :-

List<WebElement> TotalColumnCount = rowElement.findElements(By.xpath("td[position() < 3]"));
Comments