简体   繁体   English

如何使用 Selenium Java 在表中查找元素的行索引

[英]How to find the row index of an element in a table using Selenium Java

I want to find the row number ( index of the row ) of an element that is present in that row using selenium java.我想使用 selenium java 找到该行中存在的元素的行号(行的索引)。

I have tried this code but it is returning 1 every time.我试过这段代码,但每次都返回 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++;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM