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