1. 程式人生 > >分享:selenium(一) xpath

分享:selenium(一) xpath

fire 內容 路徑 補充 stty 屬性 org pre tid

xpath無所不能定位

https://www.w3.org/TR/xpath/all/#axes

兩個神器:firebug、xpath-checker

舉例:混合定位

//td[a//front[contains(text(),"從零開始視頻")]//input[@type=‘checkbox‘]

確認xpath是否是正確的:firefox==>F12==>控制臺==》$x(".//*[@id=‘su‘]")

第一部分:基礎定位:

1。依靠自己屬性,文本定位(當f元素的文本text(),屬性@是唯一時用)

//td[text()=‘wangm‘] #文本是唯一的td元素

//div[contains(@class,‘cux-rightarrowicon-on‘)] #class屬性的值是唯一的div元素

//input[@type=‘radio‘ and @value=‘1‘] #兩個屬性一起確定一個元素

2。依靠父節點定位(當子節點沒有唯一可定位的屬性,但它的父節點卻有唯一的屬性時用)

//div[@class=‘wangm‘]/div

//div[@id=‘wangm‘]/div

// div[@id=‘testid‘]/input

3。 依靠子節點定位(當父節點沒有唯一定位的屬性,但它的子節點有確定的值或者有確定的組合時)

//div[div[@id=‘wangm‘]] #中括號是用來描述父子關系的。

//div[div[@name=‘wangm‘]]

//div[p[@name=‘testp‘]]

4。 混合型(實際應用中復雜些)

//div[div[@name=‘listType‘]]/img #先用name屬性找到子節點div,定位其父div,找的是父div下的img元素。

第二部分:進階定位:

兄弟姐妹節點

following-sibling #後面的兄妹節點

preceding-sibling #

starts-with #以什麽內容開頭 語法與contains一樣。

contains #包含什麽內容

not

//input[@id=‘1234‘] /following-sibling::input #後面的兄妹input節點

//input[@id=‘1234‘] /preceding-sibling::span #前面的兄妹sapn 節點

//input[starts-with(@id,123‘)] #以123開頭的Id屬性的內容的input元素。

//span[not(contains(text(),‘xpath‘))] #尋找text內容不包含xpath的span元素。

補充整理:

絕對路徑 :html/body/div/span[2]/input[4] 中間結構變化,就失效

相對路徑: //開始 在整個html source裏找,不管在什麽位置

索 引:[x] 如://div/input[2] #div下面第2個input

誤解://span[28] #一個頁面上雖然有28個span,但只有放在同一個根節點下才是span[28]能定位到。

關鍵字:position:索引也可以認為是一個position

last

//div[@id=‘test‘]/span

分享:selenium(一) xpath