xpath選擇器

1、xpath中節點關係html

父(Parent):每一個元素以及屬性都有一個父函數

子(Children):元素節點可有零個、一個或多個子ui

同胞(Sibling):擁有相同的父的節點spa

先輩(Ancestor):某節點的父、父的父code

後代(Descendant):某個節點的子,子的子orm

 

2、xpath中選取節點的路徑表達式htm

絕對路徑blog

// 相對路徑ip

.  選取當前節點ci

..  選取當前節點的父節點

@ 選取屬性

 

3、xpath中「謂語」

放在[]中的幾種查找方式寫法以下:

//ul/li[1] , 表示選擇 ul多個li子元素中的第一個

//ul/li[last()], 表示選擇ul子元素中的最後一個li元素

//ul/li[last()-1], 表示選擇ul子元素中的倒數第二個li元素

//ul/li[position()<3],表示選取最前面2個屬於ul元素的li子元素

//a[@href] ,表示選取只要含有href屬性的a元素

//a[@href='http://www.cnblogs.com/jenniferhuang'] ,屬性值徹底匹配

//input[contains(@id,'quantityTextBox')], 當屬性值部分匹配時,插入函數的寫法

 

4、xpath函數 

函數被分紅四類

節點集函數:    last(), position(), 

字符串函數:   contains(),  substring(@class,'abc')="",  substring-before(), substring-after(),  string-length()

布爾函數:  

數字函數: 

reference : http://www.cnblogs.com/cxd4321/archive/2007/09/24/903869.html  SearchKkeyword: "核心函數庫" 

 

5、xpath軸

軸名稱

結果

ancestor

選取當前節點的全部先輩(父、祖父等)

ancestor-or-self

選取當前節點的全部先輩(父、祖父等)以及當前節點自己

descendant

選取當前節點的全部後代元素(子、孫等)

descendant-or-self

選取當前節點的全部後代元素(子、孫等)以及當前節點自己

parent

選取當前節點的父節點

child

選取當前節點的全部子元素

following

選取文檔中當前節點的結束標籤以後的全部節點

preceding

選取文檔中當前節點的開始標籤以前的全部節點

preceding-sibling

選取當前節點以前的全部同級節點

attribute

選取當前節點的全部屬性

namespace

選取當前節點的全部命名空間節點

self

選取當前節點

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

6、應用舉例

 一、定位某個節點,該節點包含有一已知的特定後代節點

//table[contains(@class,'orderHistoryBox') and descendant::dd[text()='11132789']]

二、定位一已知節點的某一先輩節點

 

//dd[text()='11132789']//ancestor::table[contains(@class,'orderHistoryBox')](與1效果相同)

三、不包含的寫法

//table[@id='wizards-ivrKeyPressAssignment-extSelectorForm-mailbox-field']/tbody/tr[not(contains(@class,'x-hidden'))][5]

 

四、注意class的取用:
//*[contains(@id,'userCallForwarding/rules-hoursSelector-tabBar-root-item') and contains(@class,'textTabButtonSelected')]

該class能夠不是當前id''userCallForwarding/rules-hoursSelector-tabBar-root-item'' 下的class,能夠是其包含標籤內其餘元素的class,例以下面對應的xpath結構

//*[contains(@id,'userCallForwarding/rules-hoursSelector-tabBar-root-item') and contains(@class,'textTabButtonSelected')]@id 

能夠取到Attribute"userCallForwarding/rules-hoursSelector-tabBar-root-item0-tabBarTextButton-text"

 

五、xpath拆分法能夠加強穩定性

例如:

    public TabBar(String id) {
        super(id);
        rootLocator = locator + PropertyReader.getProperty("Controls.TabBar.root");
        itemLocator = rootLocator + PropertyReader.getProperty("Controls.TabBar.item");
    }

 

六、xpath帶有變量

例如:

extensionDescription.xpath=//tbody[@id='system-extensions-usersGrid-tbody']//div[@class='system-extensions-title' and text()='$VALUE']/../../following-sibling::*[1]

真正要取列表中哪一個Description:

extensionDescription.getxPath().replace("$VALUE",groupName)

 

七、取屬性值

id=entry-settings-phoneSystem-departments@class

或者 //*[@id='entry-settings-phoneSystem-departments']@class

 

http://www.51testing.com/?uid-79191-action-spacelist-type-blog-itemtypeid-25252

WebElement label = driver.findElement(By.xpath("//label[text()='User Name:' and not(contains(@style,'display:none'))]"))

 

More examples:

http://www.zvon.org/xxl/XPathTutorial/General_chi/examples.html

相關文章
相關標籤/搜索