selenium如何定位同级节点

场景:当定位某个元素时,发现所需要的元素在同级节点,可以用/following-sibling::*  方法(定位同级的第二位)

     当定位统计节点的第二个定位相邻节点。 可以用/preceding-sibling::* (定位同级的第一位)

selenium定位父子、兄弟、相邻节点定位方法。

案例(一):需要定位到关闭按钮。

          

Xpath写法:

.//span[@id='ui-id-1']//following-sibling::*
 
二、定位Table 的Xpath 定位。‘
       Xpath  写法: //table[contains(@class,'hover table-Center')]/tbody/tr
    public static String getIndex(String tableXpath, String column, WebDriver driver) {
        List<WebElement> headList = driver.findElements(By.xpath(tableXpath + "/thead//td"));
        for (int i=0;i<headList.size();i++) {
            if (headList.get(i).getAttribute("innerText").trim().equals(column)) {
                return String.valueOf(i+1);
            }
        }
        throw new RuntimeException("找不到列名: " + column);
    }
原文地址:https://www.cnblogs.com/Shanghai-vame/p/8125714.html