XPATH Tutorial in Selenium

Hierarchical Structure

In this XPATH tutorial in selenium, we are going to discuss about the basics of XPATH and its basic and advance usage (for locating dynamic elements) in Selenium. XPATH is one of the locator strategies in selenium to locate web elements. Once an element is located, test cases can be applied on it.

In our Selenium 3.0 Tutorials series, we have a chapter on basics of locators. If you are new to locators, you will find it useful.

XPATH tutorial

What is XPATH?

XPath is one of the most widely used locator strategies in selenium. XPath is a W3C recommendation and uses XML document syntax for locating elements. It defines the path to any element using XML path language (that’s why it is known as XPATH).

FirePath is a simple tool to check the XPATH of any element. Firepath can be installed as an extension or add-on to the browsers.

XPATH uses a hierarchical structure to represent the location of an element, as shown below:

/html/body/a

This XPATH is for the anchor tag (or the link for “Techcanvass home page”)  in the web page shown below.

Example for XPATH tutorial

What’s the purpose? If you would like to click on this link using selenium script, you need to locate the URL, right? To understand the XPATH, let’s now look at the HTML code,

<html>
<body>
There is unexpected error. Please wait for 10 seconds to be re-directed automatically or click on<a href=”http://techcanvass.com/index.aspx”>Techcanvass Home Page</a>.</p>
</body>
</html>

The URL is put in an anchor tag (<a), which is within the <body> tag. So the XPATH for this element is

/html/body/a

As this XPATH represents the complete path of the element, it is known as absolute XPATH. Relative XPATH does not start with <HTML> (i.e. from the top of an HTML document), instead it starts from the middle.

Another difference between relative and absolute XPATH is the use of // instead of /. You can check the basics of locators chapter to know more.

Locating Dynamic elements with XPATH

We can locate an element easily if we know the exact name and location in the HTML document. But if the element name is not fixed, instead it is dynamic? How will we then locate it?

One of the techniques to locate dynamically named elements is to use contains method of web driver.

Using Contains

By using the above function user can locate any element which matches the particular text value matching a particular pattern. Let’s take an example to understand it:

 

XPATH tutorial 2

In this case, we want to locate the name field. Its name in the HTML contains “Name” but is generated dynamically by the script. So we can’t use an absolute path. So, how do we do it?

driver.findElement(By.xpath(“//input[contains(@id,’Name’)]”) ;

In the above piece of code, Webdriver code will locate an element id which contains the text ‘Name’. You must note that this may result in multiple elements, all of which contains ‘name’.

This technique is particularly useful when a web page is generated dynamically.

Using Starts-with

By using the above function user can locate any element which starts a word or a number of words.

driver.findElement(By.xpath(“//input [starts-with(@id,’txt’)]”) ;

In the above example webdriver will locate an element which starts with id as ‘txt’. There are two arguments in this piece of code. @id is the locator which is to be used for matching a search pattern. The other argument is the matching pattern – in this case it is ‘txt’.

So, what will this code do? It will find all the elements where ID has ‘txt’.

Using Ends-with

By using the above function user can locate any element which ends with a word or a number of words. Let’s look at the image below to check the class name of button having a label as “Submit”.

XPATH tutorial

We can locate the button using the fact that the class name ends with a word ‘primary’. Here is the code:

driver.findElement(By.xpath(“//input[ends-with(@class,’-primary’)]”);

 

In the next part of this tutorial, we will talk about locating elements. which are deep inside nested DIV. We will be using methods like CHILD and ANCESTOR.

Do share your feedback on the tutorials, Techcanvass is publishing for the benefit of learners.

About Techcanvass

Techcanvass offers IT certification courses for professionals. We are an IIBA endorsed education provider (EEP), iSQI ATP (for Certified Agile Business Analyst Training) as well as Agile Testing alliance partner for CP-SAT certification training in Selenium.

Techcanvass offers following courses on Automation Testing, click on them to explore further

Selenium Training Course
Selenium 3.0 Course
python selenium training
Python Selenium Course
Selenium Certification Course
CP-SAT Certification Course
Agile Automation Testing Training
Agile Automation Testing Training

 

Leave a Reply

Your email address will not be published. Required fields are marked *