Lets have a look into advance locator XPath

Reading Time: 3 minutes


In Selenium automation, if the elements are not found by the general locators like id, class, name, etc. then XPath is used to find an element on the web page . In this blog we are going to focus on advanced locators functions like Contains, Sibling, Ancestor & Selenium AND/OR, child, parent ,they are the functions from XPath 1.0 library. We have already discussed about locators and xpath in our previous blog . You can have a look here. With the combination of these functions, we can create more specific XPath in selenium. Few of them are :-

Contains() in Selenium

Contains() is a function within Xpath expression used to identify an element, when we are familiar with some part of the attributes value of an element. We can extract all the elements that match the given text value using the XPath contains() function throughout the webpage. Contains can find the elements with partial text.

The syntax for locating elements through XPath- Using contains() method can be written as:


//<HTML tag>[contains(@attribute_name,'attribute_value')] 

or

//*[contains(@attribute_name,'attribute_value')]   

Using XPath- conatins() method, we can write the Java code along with the dynamic XPath location as:

findElement(By.xpath("//a[contains(text(),'LEARN MORE')]")

Sibling In Selenium

A Sibling in Selenium Webdriver is a function used to fetch a web element which is a sibling to the parent element. It identifies the siblings of the context node. The siblings should be located at the equal level of the existing node and should have the same parent.

followingcontains all nodes which occur after the context node, in document order
following-siblingSelects all siblings after the current node
precedingcontains all nodes which occur before the context node, in document order
preceding-siblingcontains the preceding siblings of the context node
//input[@id='email']/following::*

//select[@id='day']/following-sibling::*

//input[@id='pass']/preceding::tr

//select[@id='item']/preceding-sibling::*

Ancestor in Selenium

To find an element on the basis of the parent element we can use ancestor attribute of XPath.The parent axis contains only a maximum of one node.

ancestorSelects all ancestors (parent, grandparent, etc.) of the current node
ancestor-or-selfSelects all ancestors (parent, grandparent, etc.) of the current node and the current node itself.
//div[.//a[text()='KNOLDUS']]/ancestor::div[@class='rt-grid-2 rt-omega']

Parent in Selenium

Parent in Selenium is a method used to retrieve the parent node of the current node selected in the web page. The parent axis contains only a maximum of one node. The parent node may be either the root node or an element node.
The root node has no parent; therefore, when the context node is the root node, the parent axis is empty. For all other element nodes the parent axis contains one node.

parentSelects the parent of the current node
parent::node()

The below example will selects the parent node of the input tag of Id='email'.
Ex: //input[@id='email']/parent::*

the above can also be re-written as 
//input[@id='email']/..

Xpath=//*[@id='rt-feature']//parent::div

Using AND and OR in Selenium

OR expression uses two conditions, whether the first condition OR second condition should be true. It is also applicable if any one of the condition is true, or maybe both. This means that any one condition should be true to find the element.

In the below XPath, it identifies the elements whose single or both conditions are true.

XPath=//*[@id=’FirstName’ or @name=’LastName’]

Highlight both elements as ‘First Name’ element having attribute ‘id’ and ‘Last Name’ element having attribute ‘name.’

AND expression uses two conditions. Both conditions should be true for finding the element. It fails to find the element if any one condition is false.

XPath=//*[@id=’FirstName’ and @name=’FirstName’]

In the below expression, we highlight the ‘First Name’ element as it has both attributes ‘id’ and ‘name.’

Xpath=//*[@type='submit' OR @name='btnReset']


Xpath=//input[@type='submit' and @name='btnLogin']

It’s time to say goodbye. I hope you have enjoyed the blog. For more such interesting blogs keep exploring Knoldus blog page

Written by 

Saumya is a Software Consultant at Knoldus Software LLP. She has done B.Tech from Quantum School of Technology, Roorkee. She has good knowledge of Devops technologies like Ansible, Terraform, Docker, Concourse, Jenkins, Kubernetes. She is very enthusiastic and energetic. Apart from technology, she is interested in various sports.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading