xpath實際應用

xpath

XPath 是一門在 XML 文檔中查找信息的語言。XPath 可用來在 XML 文檔中對元素和屬性進行遍歷。

xpath在selenium主要應用於無id,且屬性值不唯一的元素定位。

1、表格(節點、文本)

clipboard.png

目標:第1行第1列

//tbody/tr[1]/td[1]

釋:xpath順序從1開始

目標:第1行7列編輯按鈕

//tbody/tr[1]/td[7]/div/button/span[contains(text(),"編輯")]  
//div[@class="el-table__fixed-body-wrapper"]/table/tbody/tr[1]/td[7]/div/span[text()="編輯"]
or 
//tbody[2]/tr[1]/td[7]/div/button/span[contains(text(),"編輯")]  

2、表格(謂語條件)

目標:最後1行倒數第二列

clipboard.png

//tbody/tr[3]/td[6]  

當行數、列數不確定時,

//tbody/tr[last()]/td[last()-1]  

3、輸入框(文本,兄弟節點)

clipboard.png

1)//*[@id="app"]/section/section/main/div[2]/form/div/div[1]/div/div/div/input
2)//input[@placeholder="請輸入搜索客戶姓名"]
3)//label[text()="姓名"]/following-sibling::div[1]/div/input

4、側欄(模糊查詢、and)

clipboard.png

//*[@id="app"]/section/section/aside/div/div[2]/div[2]
//div[contains(@class,"child") and contains(text(),"團隊列表")] 

5、獲取元素class值.get_attribute(‘class’)(或:or)

clipboard.png

1)//tbody/tr[1]/td[1]/div/div/span[@class='el-tag el-tag--success el-tag--mini' or @class='el-tag el-tag--mini']
2)//tbody/tr[1]/td[1]/div/div/span[contains(text(),"發佈")]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章