XPath編寫規則學習
XPath編寫規則學習
輔助工具:firefox安裝findbugs,view Xpath
firefox :Xpath驗證方式:$x("xpath"); 粘貼xpath語句回車即可
定位:
1、依靠自己屬性,文本定位:
//td[text()='test']
//div[contains(@class,'test')]
//input[@type='radio' and @value='1']
//span[@name='bruce' and text()='bruce2'] 或 //span[@name='bruce'][ text()='bruce2'] --and關鍵字
//span[@name='bruce' or text()='bruce2'] --or關鍵字
2、依靠父節點定位:
//div[@class='test mytest']/div
//div[@id='test']/div
3、依靠子節點定位
//div[div[@id='test']] --尋找含有id=test的div的div
//div[div[@name='test']]
//div[p[@id='test']]
4、混合型定位
//div[div[@name='test']]/img
//td[a//font[contains(text(),'test')]]//input[@type='checkbox']
5、高級方法
(1)following-sibling ---尋找緊跟定位到的元素的下一個元素
例子://input[@id='1234']/following-sibling=input --定位緊跟id=1234的下一個的input元素,同級有效
//input[@id='1234']/following-sibling::input ,input後可再跟條件
(2)preceding-sibling ---尋找緊跟定位到的元素的上一個元素
例子://input[@id='123']/preceding-sibling=span --定位緊跟id=123的上一個span元素
//input[@id='1234']/preceding-sibling::input ,input後可再跟條件
(3)starts-with --判斷是否以某關鍵字開頭
例子://input[starts-with(@id,'test')]
(4)contains -- 是否包含某關鍵字
例子: //td[a//font[contains(text(),'test')]]//input[@type='checkbox']
(5)not ---不包含某關鍵字
例子://input[not(@id='1234')]
//span[not(contaions(text(),'xpath'))]
6、索引關鍵字,position,last
(1)position()=2
position()>3
position()<5
例子://div[@id='test']/span[2]或
//div[@id='test']/span[position()=2] --正數第2個span
(2)last()-1
例子://div[@id='test']/span[last()-2] --倒數第2個span元素
7、根據屬性定位
//div[@class] --查找含有class屬性的div
//div[@class='test'] --查找含有class屬性且class屬性值爲test的的div元素
8、不常用關鍵字
(1)substring,語法:substring(str,start_postion,length) ,從1開始計算
例子://div[@id='test']/span[substring(@name,3,5)='bruce'] --找name的第三位開始總共5位字母爲bruce的span
(2)substring-before ,語法:substring-before(str,substr)
例子://div[@id='test']/span[substring-before(@class,'-')='spanclass'] --查找分割關鍵字前面的字符爲spanclass的span
(3)substring-after,語法:substring-after(str,substr)
例子://div[@id='substring']/span[substring-after(@class,'-')='spanclass'] --查找分割關鍵字後面的字符爲spanclass的span
9、通配符:*
//span[@*='bruce']
//*[@*='bruce']
//*[@name='bruce']
10、axes 軸
(1)parent 父節點
例子://div[span[text()='+++test']]/parent::div[contaions(text(),'test')] --查找含有span的text爲+++test的的div的父節點
//div[span[text()='+++test']]/parent::div/span[contaions(text(),'test')]
(2)ancestor 祖先節點
例子://div[span[text()='+++test']]/ancestor::div
(3)descendant 孫子節點
例子://div[span[text()='+++test']]/descendant::div --會將該節點下的所有div打印出來
//div[span[text()='+++test']]/descendant::div/span[contaions(text(),'test')]
(4)following 將當前節點下後面所有的指定節點取出
例子://div[text()='current NodeA']/following::div --會將current NodeA後面的所有的div取出來,後續的div可再加條件判斷
(5)preceding 將當前節點下前面所有的指定節點取出
例子://div[text()='current NodeA']/preceding::div --會將current NodeA前面的所有的div取出來,後續的div可再加條件判斷
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.