xpath
XPath 是一門在 XML 文檔中查找信息的語言。XPath 可用來在 XML 文檔中對元素和屬性進行遍歷。
xpath在selenium主要應用於無id,且屬性值不唯一的元素定位。
1、表格(節點、文本)
目標:第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行倒數第二列
//tbody/tr[3]/td[6]
當行數、列數不確定時,
//tbody/tr[last()]/td[last()-1]
3、輸入框(文本,兄弟節點)
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)
//*[@id="app"]/section/section/aside/div/div[2]/div[2]
//div[contains(@class,"child") and contains(text(),"團隊列表")]
5、獲取元素class值.get_attribute(‘class’)(或:or)
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(),"發佈")]