How to customize xpath to identify webelements in smart way


What is Xpath ?
XPath is normally called as XML Path.We use XPath to navigate through elements, and attributes in the XML document.As per W3C’s, the XPath is a major element in XSLT standard – and XQuery and XPointer are both built on XPath expressions.XPath is the language for finding information in an XML document. There few types of xpath based on the html structure (Lets say the address of the element).

Absolute XPath
Absolute XPath starts with the root node or a forward slash (/).
The advantage of using absolute is, it identifies the element very fast.In practical terms, This xpath gives you the complete Address of the location..Lets assume you’re in a bar.This xpath gives the address as ‘canada/quebec/block-176/chuckles bar’

Relative Xpath
A relative xpath is one where the path starts from the node of your choise – it doesn’t need to start from the root node.

It starts with Double forward slash(//).In general terms,this xpath gives your trimmed location.Lets assume you’re in a bar.This xpath gives the address as ‘.//*(@id=’bar’)’

Here few examples with different axis of Xpath

contains()
//div[contains(@id,’module’)]
//*[@id=’regular-expression-syntax’]//h2[contains(text(), ‘7.2.1. Regular Expression Syntax’)]

concat()
concat(//*[@id=’checking-for-a-pair’]/h3/text(), //*[@id=’simulating-scanf’]/h3/text())

normalize-space()
//*[@id=’sb_form_q’][normalize-space(@value)=’asd asd’]

starts-with()
//div[starts-with(@id,’module’)]
string-length()
The string-length returns the number of characters in the string
string-length(//*[@id=’regular-expression-syntax’]/h2/text())

substring()
substring(//*[@id=’regular-expression-syntax’]/h2/text(), 2, 30)

substring-after()
//div[substring-after(@id,’finding-‘)]
//*[starts-with(@id,’finding’) and substring-after(@id,’finding-all-adverbs-‘)]

substring-before()
//div[substring-before(@id,’objects’)]
ancestor
Ancestor lets you select any ancestors [e.g., Parent and Grandparent] of the current node.
//*[@id=’regular-expression-syntax’]/ancestor::div[5]/div[2]
//*[@id=’regular-expression-syntax’]/ancestor::*

ancestor-or-self
Ancestor-or-self lets you select any ancestors [e.g., Parent and Grandparent] of the current node including the current node.
//*[@id=’regular-expression-syntax’]/ancestor-or-self::div[1]
//*[@id=’regular-expression-syntax’]/ancestor-or-self::*

attribute
Attribute returns all the attributes in the current node.
//*[@class=’sphinxsidebarwrapper’]/attribute::*
//*[@id=’sidebarbutton’]/attribute::title

child
Child returns all the children in the current node.
//*[@class=’sphinxsidebarwrapper’]/child::*
//*//child::h3

descendant
Descendant lets you select all descendants [e.g., Children and Grandchildren] of the current node.
//*[@class=’this-page-menu’]/descendant::*
//*[@class=’this-page-menu’]/descendant::li[2]
//*[@class=’documentwrapper’]/descendant::div[position()=3]

descendant-or-self
Descendant-or-self lets you select all descendants [e.g., Children and Grandchildren] of the current node including the current node.
//*[@class=’this-page-menu’]/descendant-or-self::*
//*[@id=’searchbox’]/descendant-or-self::form[@class=’search’]/input[2]

following
Following returns all in the document after the closing tag of the current node.
//*[@class=’clearer’]/following::*

following-sibling
Following-sibling returns all the sibling after the closing tag of the current node.
//*[@class=’related’]/following-sibling::*
//*[@class=’related’]/following-sibling::div[3]

 

Advertisements

One thought on “How to customize xpath to identify webelements in smart way

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s