Thursday, July 20, 2023

Basic Syntax Of XPath For Beginners

 

  1. Basic format of XPath

Format: //tagname[@Attribute=’Value’]

    Ex: //input[@id=’first_name’]


  1. Absolute XPath starts with the root node. Single forward slash (/).

Ex: /html/body/div/div[3]/div


  1. Relative XPath starts from the middle of the HTML DOM structure. Begins with double forward slash (//).

Ex: //input[@name=’user[first_name]’]



  1. 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’)]


  1. XPath function - “contains”

“contains” function is helpful in finding dynamic web elements. 

Format: //tagname[contains(@Attribute,’Value’)]

Ex: //input[contains(@name,’email’)]


  1. XPath function - “text()”

“text()” method is used to find elements with exact text matches. 

Format: //tagname[text()=’ActualText’)]

Ex: //a[text()=’Terms_Of_Use’]


  1. 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]’]


  1. 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]’]


  1. XPath function - parent

Selects the parent of the context (current) node.

Format: //tagname[@Attribute=’Value’]//parent::tagname

Ex: //select[@name=’user[country]’]//parent::form


  1. XPath function - child

Selects all children of the current node.

Format: //tagname[@Attribute=’Value’]//child::tagname

Ex: //select[@name=’user[country]’]//child::option[1]


  1. XPath function - self

Selects the current node.

Format: //tagname[@Attribute=’Value’]//self::tagname

Ex: //select[@name=’user[country]’]//self::select


  1. 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


  1. 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


  1. 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


  1. 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


  1. 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


  1. 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


  1. 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


  1. 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


No comments:

Post a Comment