首页 > 技术文章 > selenium+java的几种xpath定位方法

silna 2018-08-24 17:22 原文

1.同级元素,弟弟定位哥哥

方法1:/后面加上“..”,表示回到父节点,回到父节点后再往下定位其他子节点

如:By.xpath("//div[contains(@id,'title:')]/../button[1]");

方法2:

preceding-sibling - 上一个同级节点

By.xpath("//div[contains(@id,'title:')]/preceding-sibling::button[1]");

 

2.同级元素,哥哥定位弟弟

方法1:先回到父节点

By.xpath("//div[@id='title')]/../button[1]");

方法2:following-sibling - 下一个同级节点

By.xpath("//div[@id='title')]/following-sibling::button[1]");

3.由子节点定位父节点

/..  ,parent::,都代表父节点,parent::*默认为第一个节点

方法1:By.xpath("//input[@id='btnRecharge']/..");

方法2:By.xpath("//input[@id='btnRecharge']/parent::div");

方法3:By.xpath("//input[@id='btnRecharge']/parent::*");

4.由父节点定位子节点

方法1:By.xpath("//div[@id='father']/child::input");

child:input默认为子节点中第一个input节点,这是不使用[]的写法,若使用[],则需要表明child::input[1]

方法2:By.xpath("//div[@id='father']/child::*");

child::*默认是子节点中的第一个节点

 

转载自:https://blog.csdn.net/amoscn/article/details/79605742

 

推荐阅读