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 选取当前节点的所有命名空间节点