Sunday, 19 May 2019

SE - 36 - Different types of Xpath in Selenium

Xpath is one of the commonly used locators in Selenium across many projects because of its flexibility in different variants. Due to these variants we get the flexibility to use xpath in different ways. Lets discuss each one of them:

1) Tag with attribute - This is the simplest form of Xpath in which we use tag with any attribute in the given html :
Syntax :
Xpath = //input[@id= 'systemId']


2) Tag with multiple attribute - We can provide multiple attributes in a tag based on requirement by using OR and AND 
Syntax :

Xpath = //input[@id= 'systemId' OR @name ='TheName']
Xpath = //input[@id= 'systemId' AND @name ='TheName']


3) Tag with any attribute - We can also locate any element in the tag by giving "*" at the place of attribute. It will search for all the attributes in the particular tag.
Syntax :
Xpath = //input[@*= 'systemId']


4) Any Tag with given attribute - We can also locate a particular element in  any of the the tag by giving "*" at the place of tag. It will search for all the tags in the particular node with the given attribute.
Syntax :
Xpath = //*[@id= 'systemId']


5) Any Tag with any attribute - We can also locate any element in any of the the tag by giving "*" at the place of tag and attribute. It will search for all the tags and all the attributes in the node.
Syntax :
Xpath = //*[@*= 'systemId']


6) Using contains method - There are many instances when the xpath keep on changing but value remains same. Sometimes the text or partial text remains same and other changes. In such situations we use contains keyword. We can also use "*" to look for all the tags else we can also give a static tag
Syntax :
Xpath = //*[contains (@*,'systemId')]
Xpath = //*[contains (@id,'systemId')]
Xpath = //input[contains(@*,'systemId')]
Xpath = //input[contains(@id ,'systemId')]
Xpath = //*[contains(text(), 'systemId')]
Xpath = //input[contains(text(), 'systemId')]

we can also use not before contains if required.
Syntax :
Xpath = //*[not(contains (@*,'systemId'))]

7) Using text method - This method allows to look for a text in any attribute to locate it. We can give full or partial text as well 
Syntax :
Xpath = //*[text() = 'systemId']
Xpath = //input[text(), 'systemId']


8) Using starts-with method - This is also similar to above methods where the xpath is not static. 
Syntax :
Xpath = //input[ starts-with (@id = 'systemId')]


9) Using last method - This is used to locate the last element of a node. we can use (last()-n) if we want to locate any element before the last one.It is mostly used in locating tr[table row] and td[table data] .
Syntax :
Xpath = //input[@id ='systemId']//tr[last()-1]


10) Using parent method - This method is used to locate a parent node through a child node. This may be used when we are not directly able to locate a object and we need to navigate through parent :
Syntax :
Xpath = //input[@id ='systemId']/parent::td/parent::tr2]


11) Using child method - This method is used to locate all the child elements from the parent or current node.
Syntax :
Xpath = //input[@id ='systemId']/child::td[2]


12) Using following method - This method is used to locate all the elements from the current node. Descendants are not selected using this method .
Syntax :
Xpath = //input[@id ='systemId']/following::td[2]


13) Using preceding method - This method is used to locate all the nodes that is before the current node. Ancestors are not selected using this method .
Syntax :
Xpath = //input[@id ='systemId']/preceding::td[2]


14) Using ancestor method - This method is used to locate all the ancestor nodes like parent grandparent that is before the current node.
Syntax :
Xpath = //input[@id ='systemId']/preceding::td[2]


15) Using following-sibling method - This method is used to locate all the following siblings which are at same level
Syntax :
Xpath = //input[@id ='systemId']/following-sibling::td[2]


16) Using preceding-sibling method - This method is used to locate all the preceding siblings which are at same level
Syntax :
Xpath = //input[@id ='systemId']/preceding-sibling::td[2]


17) Using descendant method - This method is used to locate all the descendant nodes from the current node like child, grandchild , etc
Syntax :
Xpath = //input[@id ='systemId']/descendant::td[2]


18) Using self method - This is used to select the current node
Syntax :
Xpath = //input[@id ='systemId']/self::td[2]

No comments:

Post a Comment