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

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

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

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

          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);
    }

 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-19
  • 2022-12-23
  • 2021-09-23
  • 2021-10-03
猜你喜欢
  • 2022-12-23
  • 2021-10-17
  • 2022-01-19
  • 2022-12-23
  • 2021-04-17
  • 2022-02-27
  • 2021-12-28
相关资源
相似解决方案