Basic format of XPath
Format: //tagname[@Attribute=’Value’]
Ex: //input[@id=’first_name’]
Absolute XPath starts with the root node. Single forward slash (/).
Ex: /html/body/div/div[3]/div
Relative XPath starts from the middle of the HTML DOM structure. Begins with double forward slash (//).
Ex: //input[@name=’user[first_name]’]
XPath function - “starts-with”
“starts with” function is helpful in finding dynamic web elements.
Format: //tagname[starts-with(@Attribute,’Value’)]
Ex: //input[starts-with(@id,’last_name’)]
XPath function - “contains”
“contains” function is helpful in finding dynamic web elements.
Format: //tagname[contains(@Attribute,’Value’)]
Ex: //input[contains(@name,’email’)]
XPath function - “text()”
“text()” method is used to find elements with exact text matches.
Format: //tagname[text()=’ActualText’)]
Ex: //a[text()=’Terms_Of_Use’]
XPath function - and
Helpful if you want to use more than two attributes to find an element on the webpage.
Format: //tagname[@Attribute=’Value’ and @Attribute=’Value’]
Ex: //input[@type=’text’ and @name=’user[first_name]’]
XPath function - or
Helpful if you want to use more than two attributes to find an element on the webpage.
Format: //tagname[@Attribute=’Value’ or @Attribute=’Value’]
Ex: //input[@type=’text’ or @name=’user[first_name]’]
XPath function - parent
Selects the parent of the context (current) node.
Format: //tagname[@Attribute=’Value’]//parent::tagname
Ex: //select[@name=’user[country]’]//parent::form
XPath function - child
Selects all children of the current node.
Format: //tagname[@Attribute=’Value’]//child::tagname
Ex: //select[@name=’user[country]’]//child::option[1]
XPath function - self
Selects the current node.
Format: //tagname[@Attribute=’Value’]//self::tagname
Ex: //select[@name=’user[country]’]//self::select
XPath function - descendant
Selects all the descendants (children, grandchildren, etc) of context (current) node.
Format: //tagname[@Attribute=’Value’]//descendant::tagname
Ex: //div[@class=’signup_container’]//descendant::div
XPath function - descendant-or-self
Selects the context (current) node and all of its descendants (children, grandchildren, etc).
Format: //tagname[@Attribute=’Value’]//descendant-or-self::tagname
Ex: //div[@class=’signup_container’]//descendant-or-self::div
XPath function - ancestor
Selects all the ancestors (parent, grandparent, etc) of context (current) node.
Format: //tagname[@Attribute=’Value’]//ancestor::tagname
Ex: //*[@id=’errorbox’]//ancestor::div
XPath function - ancestor-or-self
Selects the context (current) node and all of its ancestors (parent, grandparent, etc).
Format: //tagname[@Attribute=’Value’]//ancestor-or-self::tagname
Ex: //*[@id=’errorbox’]//ancestor-or-self::div
XPath function - following
Selects all the nodes that appear after the context (current) node.
Format: //tagname[@Attribute=’Value’]//following::tagname
Ex: //option[@value=’Developer’]//following::option
XPath function - following-sibling
Selects all the nodes that have the same parent as the context (current) node and appear after the context (current) node.
Format: //tagname[@Attribute=’Value’]//following-sibling::tagname
Ex: //option[@value=’Developer’]//following-sibling::option
XPath function - preceding
Selects all the nodes that appear before the context (current) node.
Format: //tagname[@Attribute=’Value’]//preceding::tagname
Ex: //option[@value=’Developer’]//preceding::option
XPath function - preceding-sibling
Selects all the nodes that have the same parent as the context (current) node and appear before the context (current) node.
Format: //tagname[@Attribute=’Value’]//preceding-sibling::tagname
Ex: //option[@value=’Developer’]//preceding-sibling::option