xpath 定位方式

xpath的作用就是兩個(gè)字“定位”,運(yùn)用各種方法進(jìn)行快速準(zhǔn)確的定位,推薦兩個(gè)非常有用的的firefox工具:firebug和xpath checker


定位

1.依靠自己屬性,文本定位

//td[text()='xxx']

//div[contains(@class,'xxx')]

//div[@class='xxx' and @type='xxx']


2.依靠父節(jié)點(diǎn)定位

//div[@class='xxx']/div

//div[@id='xxx']/div



3.依靠子節(jié)點(diǎn)定位

//div[div[@id='xxx']]

//div[div[@name='xxx']]


4.混合型

//div[div[@name='xxx']]/img

//td[a/font[contains(text(),'xxx')]]//input[@type='xxx']


xpath的學(xué)習(xí)-拓展

1.following-sibling

following-sibling即為“選擇當(dāng)前節(jié)點(diǎn)之后的所有同級(jí)節(jié)點(diǎn)”,那么沒(méi)有加上“sibling”關(guān)鍵字的,搜索的就是之上/之下的所有節(jié)點(diǎn),忽略同級(jí)概念,例如:

? ?

要定位第二個(gè)input://input[@id='123']/following-sibling::input


2.preceding-sibling

preceding-sibling的解釋是“選取當(dāng)前節(jié)點(diǎn)之前的所有同級(jí)節(jié)點(diǎn)”,那么沒(méi)有加上“sibling”關(guān)鍵字的,搜索的就是之上/之下的所有節(jié)點(diǎn),忽略同級(jí)概念,? ???? ? ??? ? ? ? ? ? ?? ??preceding-sibling和following-sibling是剛好相反的

text

? ?

要定位第二個(gè)input://input[@id='123']/preceding-sibling::span


3.contains

和字面意思一樣就是包含,例如://div[contains(@class,'xxx')]


4.starts-with

和字面意思一樣就是以某某開(kāi)頭,例如://input[starts-with(@class,'xxx')]


5.not

就是否定的意思

比如找一個(gè)id不為123的input:input[not[id='123']]

又如找一個(gè)文本中不包含xxx字段的span://span[not(contains(text(),'xxx'))]


xpath的學(xué)習(xí)-補(bǔ)充

絕對(duì)路徑html/body/div/span[2]/input[2] 中間結(jié)構(gòu)變化,就失效

相對(duì)路徑//開(kāi)始,在整個(gè)html source里找,不管在什么位置


索引[x]//div/input[2] div下面第二個(gè)input


position()=2position()>3position()<3

例如html:

test position()1

test position()2

test position()3

test position()4

test position()5

獲取第一個(gè)span,可以是//div[@id='positions']/span[1],也可以是//div[@id='positions']/span[position()=1]

//div[@id='positions']/span[position()>3]就是定位了test position()4和test position()5

//div[@id='positions']/span[position()<3]就是定位了test position()1和test position()2


last()last()-1

以上面的html為例子,獲取最后一個(gè)span://div[@id='positions']/span[last()]

以上面的html為例子,獲取倒數(shù)第二個(gè)span://div[@id='positions']/span[last()-1]


屬性定位@class ? //div[@class] ?有class屬性的div

屬性值定位,前面已經(jīng)講過(guò)了 ?//div[@class='xxx']


功能關(guān)鍵字

1.常用

and/[][],比如://span[@name='xxx' and text()='xxx']也是可以寫(xiě)成//span[@name='xxx'][text()='xxx']

or,比如以上面html為例子,定位文本為test position()5和test position()4的span://div[@id='positions']/span[text()='test position()5' or text()='test position()4']

not,contains,starts-with

ends-with 在xpath中是沒(méi)有這個(gè)的

2.不常用的

substring,substring-before,substing-after


sbustring(str,start-position,length)比如html:

text

定位上面html中span://div[@id='xxx']/span[substring(@name,3,5)='xxxxx']


substring-before的用法,比如html

text

定位上面html中span://div[@id="xxx"]/span[sbustring-before(@class,"-")="spanclass1"]


substring-after的用法,比如html

text

定位上面html中span://div[@id="xxx"]/span[sbustring-after(@class,"-")="spanclass22"]

通配符 ?*

比如//span[@*="xxx"]指定位span中任意屬性包含xxx的

比如//*[@*="xxx"]指定位頁(yè)面中任意屬性保護(hù)xxx的標(biāo)簽


Axes 軸

parent 父節(jié)點(diǎn)

ancestor 祖先節(jié)點(diǎn),包括父節(jié)點(diǎn),一層一層向上

descendant 所有子孫節(jié)點(diǎn)找,不管什么位置,簡(jiǎn)寫(xiě)//,就是xpath中出現(xiàn)//的情況。。//div[@class="xxx"]//input

follwing-sibling 當(dāng)前元素后面的兄弟姐妹

preceding-sibling 當(dāng)前元素前面的兄弟姐妹

following 當(dāng)前元素后面所有元素,一直到

preceding ?當(dāng)前元素之前所有元素,一直到

ancestor-or-self

descendant-or-self

使用的時(shí)候注意加上::

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,653評(píng)論 19 139
  • 使用selenium進(jìn)行自動(dòng)化時(shí)少不了對(duì)元素進(jìn)行定位,但目前前端大多使用框架vue,angular等,很多元素并沒(méi)...
    菠了個(gè)蘿閱讀 10,038評(píng)論 1 5
  • 每個(gè)人都不會(huì)花太多精力走進(jìn)別人的世界,并試圖去理解。 文│曦明 “所謂的旅游就是從自己呆膩的地方,去別人呆膩的地方...
    KSZxMX閱讀 707評(píng)論 0 8
  • 輕輕抬首,三亞的玲瓏清月,沉溺在了骯臟的濃霧中,自歡。圣潔的光輝因此可惜的被黑暗隱晦了。今夜,我借來(lái)些廉價(jià)的酒,...
    在月下閱讀 447評(píng)論 1 5
  • 同學(xué)找我聊天,我說(shuō)我在上課,沒(méi)時(shí)間。他說(shuō)你都多大年紀(jì)了,還學(xué)瑜伽,那么臭美干嘛? 我一驚,是?。≡诤芏嗄腥说难壑?..
    生之惑閱讀 2,074評(píng)論 24 29

友情鏈接更多精彩內(nèi)容