1. 绝对路径
# /是绝对路径定位
/html/body/div[1]/div[1]/div[5]/div/div/form/span[1]/input
2. 相对路径
# //是相对路径定位
//span/input
3. 索引定位
//form/span[1]/input
4. 属性定位
# 属性可以放在对应的路径层级中使用
//input[@autocomplete="off"]
//div[@class="ivu"]/div/div/div[2]/div[2]/button[1]
5. 通配符定位,通配符就是*,*就是任何一个都可以的意思
# 任意一个标签,只要符合后面的属性就可以
//*[@id="kw"]
# input标签,不管什么属性只要属性符合就可以
//input[@*="kw"]
6. 部分属性,模糊匹配
# 包含f的autocomplete属性
//input[contains(@autocomplete,"f")]
# 以of为开头的autocomplete属性
//input[starts-with(@autocomplete,"of")]
# autocomplete的属性,属性内容从第二个字母开始到结束为ff,这个属性内容必须写到结尾
//input[substring(@autocomplete,2)="ff"]
7. 文本定位
# 文本定位的写法与属性定位的写法不同,前面没有@,text后要有()
//span[text()="按图片搜索"]
# 文本内容的模糊匹配,也是前面没有@,text后要有()
//span[contains(text(),"搜索")]
8. 同时用两种属性定位
# and写法
//a[text()="更多" and @class='s-bri c-font-normal c-color-t']
# 两个[][]写法
//a[text()="更多"][@class='s-bri c-font-normal c-color-t']
# and写法,class属性内容模糊匹配
//a[text()="更多" and contains(@class,"s-bri")]
# 两个[][]写法,class属性内容模糊匹配
//a[text()="更多"][contains(@class,"s-bri")]
9. 匹配同级另一个属性
# 元素的同级,下面第一个input节点
//span[@class="bg s_btn_wr"]/following-sibling::input[1]
# 元素的同级,上面第一个input节点
//span[@class="bg s_btn_wr"]/preceding-sibling::input[1]
10. 匹配所有后代节点
# 选取元素form[@id="form"]的所有后代元素节点中input[@id="kw"]
//form[@id="form"]/descendant::input[@id="kw"]
11. 匹配所有先辈节点
# 选取元素input[@id="kw"]的所有先辈元素节点中div[@class="s_form s_form_nologin"]
//input[@id="kw"]/ancestor::div[@class="s_form s_form_nologin"]
12. 匹配直属父节点
# parent定位方法,元素的直属父级form节点
//span[@class="bg s_btn_wr"]/parent::form
# ..定位方法
//span[@class="bg s_btn_wr"]/..
13. 匹配元素位置
# 第一个元素
//*[@id="common-header"]/div[1]
# 最后一个元素
//*[@id="common-header"]/div[last()]
# 倒数第二个元素
//*[@id="common-header"]/div[last()-1]