我想用selenium java来查找存在于该行的元素的行号(行的索引)。
我试过这段代码,但每次都返回1。
int getElementIndex(WebElement element) {
WebElement parent = element.findElement(By.xpath(".."));
ArrayList<WebElement> siblings = (ArrayList<WebElement>) parent.findElements(By.xpath("./" + element.getTagName()));
int i=0;
for (WebElement sibling : siblings) {
if (element.equals(sibling)) {
System.out.println("Calling from function"+i);
return i;
} else {
i++;
}
}
throw new NotFoundException(); // Should never happen
}
WebElement parent = element.findElements(By.xpath(".."));
List<WebElement> siblings = parent.findElements(By.xpath("you need to correct your xpath here it looks suspicious"));
for (WebElement sibling : siblings) {
if (element.equals(sibling)) {
System.out.println("Calling from function"+i);
return i;
} else {
i++;
}
}