如何写xpath的小笔记

1. Fuzzy search by fuzzy hierarchy

//tagName[@attributeName='attributeValue']

For examples:
For following html:

<td name='hello'>
<input type="text" maxlength="25" name="userId" size="25" id="userId" class="inputBox" value='hello' />
 </td>

 Xpath of input node could be: 
//input[@name='userId']
//td/input[@name='userId']

2. Fuzzy search by fuzzy hierarchy and fuzzy tag name

//*[@id='id']

For examples:
For following html :

<td name='hello'>
    <td>
<input type="text" maxlength="25" name="userId" size="25" id="userId" class="inputBox" value='hello' />
    </td>
</td>

Xpath of input node could be: 
//*[@name='userId']
//*[@name='hello']//*[@value='hello']

3. Search by one exact attribute value
The attribute could be id, class, type, name, value etc. which current node contains.

//tagName[@attributeName='attributeValue'] 
//*[@attributeName='attributeValue']

For examples:
For following html :

<input type="text" maxlength="25" name="userId" size="25" id="userId" class="inputBox" value='hello' />

Xpath of input node could be: 
//input[@id= 'userId']
// input [@class=' inputBox']
// input [@type=' text']
// input [@name=' userId']
//*[@maxlength= ‘25']
//*[@value= ‘hello']

4. Search by more than one attributes

//tagName[@attributeName='attributeValue' and @attributeName=' attributeValue']
//*[@attributeName='attributeValue' and @attributeName=' attributeValue']

For examples:
For following html :

<input type="text" maxlength="25" name="userId" size="25" id="userId" class="inputBox" value='hello' />

Xpath of input node could be: 
//input[@id= ‘userId' and @class=' inputBox']
// * [@class=' inputBox' and @type=' text']

5. Search by displayed text of one element

//tagName[contains(text(), 'displayedText')]")

For examples:
For following html :

<td class="portletFieldLabel" nowrap="nowrap" align="right"> User Name: </td>

Xpath of td node could be: 
//td[contains(text(), 'User Name:')]
//*[@class='portletFieldLabel' and contains(text(), 'User Name')]
//td[@class='portletFieldLabel' and contains(text(), 'User Name')]

6. Search by exact attribute value and not contain one attribute

//*[@attributeName='pagin' and not(@attributeName)]

For examples:
For following html :

<td class="portletFieldLabel" nowrap="nowrap" align="right"> User Name: </td>
<td class="portletFieldLabel"  align="right"> User Name: </td>


Xpath of second node which not contains attribute nowrap could be: 
//td[contains(text(), 'User Name:') and not(@nowrap)]
//*[@class='portletFieldLabel' and not(@nowrap)]

7. For XPath defined, usually prefer to only match one node in the page, when no way to match only one, you can use [matchNodeIndex] to indicate one of the matched nodes.

For examples:
For following html :

<input type="text" maxlength="25" name="userId" size="25" id="userId" class="inputBox" value='hello' >
<input type="text" maxlength="25" name="userId" size="25" id="userId" class="inputBox" value='hello' />
<input type="text" maxlength="25" name="userId" size="25" id="userId" class="inputBox" value='hello' />

Xpath of first input node could be: 
//input[@id='userId'][1]
Xpath of second input node could be:
//input[@id='userId'][2]

Note: samples

//*[@class = 'mainPortletWrapper']//button[contains(text(), 'Activate session')]")
//*[@id='roleSelectorBean']//*[@class='portletIntroInfo']")
//*[@for='allowPrincipalOverride1']
//*[@id='roleSelectorBean']//*[@name='cancelBtn']
//foo[@style='']
//*[@id='resultsList']//div[@class='pagin' and not(@style)]//*[@class ='mapPin']
//*[@ID='mapResultsContainer']//*[@class='pagin' and not(@style)]//*[@class='dlrInfo'])[1]

XPath轴(XPath Axes)可定义某个相对于当前节点的节点集:
1、child 选取当前节点的所有子元素
2、parent 选取当前节点的父节点
3、descendant 选取当前节点的所有后代元素(子、孙等)
4、ancestor 选取当前节点的所有先辈(父、祖父等)
5、descendant-or-self 选取当前节点的所有后代元素(子、孙等)以及当前节点本身
6、ancestor-or-self 选取当前节点的所有先辈(父、祖父等)以及当前节点本身
7、preceding-sibling 选取当前节点之前的所有同级节点
8、following-sibling 选取当前节点之后的所有同级节点
9、preceding 选取文档中当前节点的开始标签之前的所有节点
10、following 选取文档中当前节点的结束标签之后的所有节点
11、self 选取当前节点
12、attribute 选取当前节点的所有属性
13、namespace 选取当前节点的所有命名空间节点

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值