[英]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
? 它说
findElements(By.xpath("td[]"))
,如何仅获取List
的td[1]
, td[2]
和td[3]
元素?
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;
}
Just add those items to your list. 只需将这些项目添加到您的列表即可。
WebElement Webtable = driver.findElement(By.xpath(""));
int num_elements_to_get = 3;
int rows = Webtable.findElements(By.xpath("tr")).size();
List<WebElement> data = new ArrayList<WebElement>();
for(int i = 1; i <= rows; i++) {
for(int j = 1; j <= num_elements_to_get; j++) {
data.add(Webtable.findElement(By.xpath("tr["+i+"]/td["+j+"]")));
}
}
Then the column elements that you want will be stored in data. 然后,所需的列元素将存储在数据中。
您应该尝试使用xpath
position()
函数,如下所示,该函数将仅为每行选择前三列:
List<WebElement> TotalColumnCount = rowElement.findElements(By.xpath("td[position() <= 3]"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.