selenium+java的幾種xpath定位方法


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM