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

问题描述 投票:0回答:1

我想用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
}
java selenium row
1个回答
0
投票
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++;
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.