xpath选择器

xpath选择器,第1张

xpath选择器


一、xpath中节点关系

父(Parent):每个元素以及属性都有一个父

子(Children):元素节点可有零个、一个或多个子

同胞(Sibling):拥有相同的父的节点

先辈(Ancestor):某节点的父、父的父

后代(Descendant):某个节点的子,子的子


二、xpath中选取节点的路径表达式

/  绝对路径

// 相对路径

.  选取当前节点

..  选取当前节点的父节点

@ 选取属性


三、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')], 当属性值部分匹配时,插入函数的写法


四、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: "核心函数库"


五、xpath轴

轴名称

结果

ancestor

选取当前节点的所有先辈(父、祖父等)

ancestor-or-self

选取当前节点的所有先辈(父、祖父等)以及当前节点本身

descendant

选取当前节点的所有后代元素(子、孙等)

descendant-or-self

选取当前节点的所有后代元素(子、孙等)以及当前节点本身

parent

选取当前节点的父节点

child

选取当前节点的所有子元素

following

选取文档中当前节点的结束标签之后的所有节点

preceding

选取文档中当前节点的开始标签之前的所有节点

preceding-sibling

选取当前节点之前的所有同级节点

attribute

选取当前节点的所有属性

namespace

选取当前节点的所有命名空间节点

self

选取当前节点


六、应用举例

1、定位某个节点,该节点包含有一已知的特定后代节点

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

2、定位一已知节点的某一先辈节点

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

3、不包含的写法

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

4、注意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"

5、xpath拆分法可以增强稳定性

例如:

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

6、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)

7、取属性值

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/588080.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-12
下一篇 2022-04-12

发表评论

登录后才能评论

评论列表(0条)

保存