×

XPath Axes Selenium WebDriver

XPath Axes

XPath axes are those axes that are used to search for the multiple nodes in the XML document from the current node context.

These methods are mainly used when the web element is not identified with the help of ID, name, class name, link text, CSS selector and XPath, etc. locators.

Since XPath axes are used to identify the web element that changes dynamically on the refresh or the dynamic operations.

Syntax for XPath axes:

//tagname/axes::target elements
XPath Axes

Some XPath axes which are commonly used to identify the web element through XPath are given below:

XPath Axes Description Syntax
Child Traverse all child element of the current html tag //*[attribute=’value’]/child::tagname
Parent Traverse all parent element of the current html tag //*[attribute=’value’]/parent::tagname
Following Traverse all element that comes after the current tag //*[attribute=’value’]/following::tagname
Preceding Traverse all nodes that comes before the current html tag. //*[attribute=’value’]/preceding::tagname
Following-sibling Traverse from current Html tag to Next sibling Html tag. //current html tag[@attribute =’value’]/following-sibling:: sibling tag[@attribute =’value’]  
Preceding-sibling Traverse from current Html tag to previous sibling Html tag. //current html tag[@attribute =’value’]/preceding-sibling:: previous tag[@attribute =’value’]  
Ancestor Traverse all the ancestor elements (grandparent, parent, etc.) of the current html tag. //*[attribute=’value’]/ancestor::tagname
Descendant Traverse all descendent element (child node, grandchild node, etc.) of the current Html tag. //*[attribute=’value’]/descendant::tagname

Now, we are discussing all the XPath one by one to give you a complete understanding of the topic.

Child:

The child XPath axes are used to locate or identify the child tags of the current html tag. 

Syntax of child XPath axes:

//*[attribute=’value’]/child::tagname

Let us take one real-time example where we will try to identify the child web element.

For the child XPath axes, we are finding the sign-up button from the registration form present in the Facebook application.

We will try to take the full form, and through the child items will come to the sign-up button.

Follow the below steps:

  • Open https://www.facebook.com URL in your Google Chrome browser.
  • And firstly, inspect the registration form and customize the XPath for it.
child XPath axes

Then using child XPath axes, we will locate the sign-up button, which is as shown below.

we will locate the sign-up button

And, after that you can copy that XPath and paste in it your java code, and our final test script will look like this:

package testpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Child_xpath_axes {
public static void main(String[] args) throws InterruptedException { 
//set the system property            
System.setProperty("webdriver.chrome.driver","C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe");
//creating the object for Chrome driver  
WebDriver driver = new ChromeDriver();
//Navigate to Facebook application 
driver.get("https://www.facebook.com");
driver.manage().window().maximize(); 
 //using the child XPath axes
driver.findElement(By.xpath("//div[@id='reg_form_box']/child::div[10]")).click();
Thread.sleep(3000);
System.out.println("child axes is clicked");
//closing the browser               
driver.close();
}
} 
  • To run the above code in the Eclipse, we have to right-click on the code and then select Run As ? Java Application.
  • The above test script will launch the Google Chrome browser and automate all the test scenarios. 
Google Chrome browser and automate all the test scenarios.

Parent

The parent XPath axes are used to locate or identify the parent tags of the current html tag. 

Syntax of parent XPath axes:

//*[attribute=’value’]/parent::tagname

Let us take one real-time example where we will try to identify the parent web element.

For the parent XPath axes, we identify the search text box from the Google search button present in the Google search home page.

First, we will try to locate the Google search button, and through the parent axes will go to the search text box.

Follow the below steps:

parent XPath axes are used to locate

And then click on the chropath and copy the relative XPath for the Google Search button.

relative XPath for the Google Search button.

And now, we are writing the XPath to locate the Google search text box using parent XPath axes.

the XPath to locate the Google search text box using parent XPath axes.

And, after that you can copy that XPath and paste in it your java code, and our final test script will look like this:

package testpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class parent_xpath_axes {
public static void main(String[] args) throws InterruptedException {
//set the system property            
System.setProperty("webdriver.chrome.driver","C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe"); 
//creating the object for chrome driver  
WebDriver driver = new ChromeDriver();
//Navigate to Google search page
driver.get("https://www.google.com");
driver.manage().window().maximize(); 
//using the parent XPath axes 
driver.findElement(By.xpath("//div[@class='FPdoLcVlcLAe']//input[@name='btnK']/parent::*/parent::*/parent::div[1]//input[@name='q']")).click();
Thread.sleep(3000);
System.out.println("parent axes is clicked");
//closing the browser                                    
driver.close();
}
} 
  • To run the above code in the Eclipse, we have to right-click on the code and then select Run As ? Java Application.
  • The above test script will launch the Google chrome browser and automate all the test scenarios.
To run the above code in the Eclipse

Following

Following XPath, axes are used to return all elements that come after the current tag.

Syntax of Following XPath axes:

//*[attribute=’value’]/following::tagname

Let us take one real-time example where we will try to identify the following web element.

For the following XPath axes, we identify the best sellers link from the amazon search text box present in the amazon home page.

First, we will try to locate the best sellers link and through the following axes.

Follow the below steps:

  • Open https://www.amazon.in URL in your Google Chrome browser.
  • And first, inspect the amazon search text box.
amazon search text box
  • And then click on the chropath and copy the relative XPath for the Amazon Search button.
relative XPath for the Amazon Search button.

And now, we are writing the XPath to locate the Google search text box using parent XPath axes.

Google search text box using parent XPath axes.

And, after that you can copy that XPath and paste in it your java code, and our final test script will look like this:

package testpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class following_xpath_axes {
public static void main(String[] args) throws InterruptedException {
//set the system property  
System.setProperty("webdriver.chrome.driver","C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe");
//creating the object for Chrome driver  
WebDriver driver = new ChromeDriver();                                                                              
//Navigate to amazon home page
driver.get("https://www.amazon.in");
driver.manage().window().maximize(); 
//using following XPath axes                  driver.findElement(By.xpath("//input[@id='twotabsearchtextbox']/following::a[contains(text(),'Best Sellers')]")).click();
Thread.sleep(3000);
System.out.println("following axes is clicked");
//closing the browser 
driver.close();
}
} 
  • To run the above code in the Eclipse, we have to right-click on the code and then select Run As ? Java Application.
  • The above test script will launch the Google Chrome browser and automate all the test scenarios.
Google Chrome browser and automate all the test

Preceding

Preceding XPath axes are used to traverse all nodes that come before the current html tag.

Syntax of Preceding XPath axes:

//*[attribute=’value’]/preceding::tagname

Let us take one real-time example where we will try to identify the web element with the help of preceding axes.

For the following XPath axes, we identify the hello sign-in linkfrom the amazon search text box present in the amazon home page.

First, we will try to locate the hello sign-in link and through the preceding axes.

Follow the below steps:

  • Open https://www.amazon.in URL in your Google Chrome browser.
  • And first, inspect the amazon search text box.
 inspect the amazon search text box.
  • And then click on the chropath and copy the relative XPath for the amazon Search button.
click on the chropath and copy the relative XPath

And now, we are writing the XPath to locate the Google search text box using parent XPath axes.

we are writing the XPath to locate the Google search text box

And, after that you can copy that XPath and paste in it your java code and our final test script will look like this:

package testpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class preceding_xpath_axes {
public static void main(String[] args) throws InterruptedException {
//set the system property            
System.setProperty("webdriver.chrome.driver","C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe"); 
//creating the object for Chrome driver  
WebDriver driver = new ChromeDriver();
//Navigate to amazon home page
driver.get("https://www.amazon.in");
driver.manage().window().maximize();            
//using preceding XPath axes              driver.findElement(By.xpath("//input[@id='twotabsearchtextbox']/preceding::span[contains(text(),'Hello. Sign in')]")).click();
Thread.sleep(3000); 
System.out.println("preceding axes is clicked");
//closing the browser 
driver.close();
 }
 } 
  • To run the above code in the Eclipse, we have to right-click on the code and then select Run As ? Java Application.
  • The above test script will launch the Google Chrome browser and automate all the test scenarios.
test script will launch the Google Chrome browser

Following-sibling:

To identify the element using siblings as a reference, we will take help from the following-sibling XPath axes.

Whenever element not able to identify using one or more attribute, parent html tag and grandparent html tag

In such cases, we can use the sibling keyword to identify the element uniquely.

The Following-sibling keyword will be used to traverse from the current html tag to the next immediate html tag.

Syntax for following-sibling

//current html tag[@attribute =’value’]/following-sibling::sibling tag[@attribute =’value’]

Let us consider the above example, where we will try to identify a sibling web element present in UI.

we will try to identify a sibling web element present in UI.

We can write the Java code along with the dynamic XPath location as:

Java code along with the dynamic XPath

driver.findElement(By.xpath("//img[@src='img1.png']/following-sibling::input[@value='add to cart']"));

 Let see a real-time example for more clarification of the following-sibling case:

Follow the steps given below:

  • Open URL: https://www.amazon.in in your Google Chrome browser.
  • Then go to the navigation bar, and take one element as current html tag,
  • In our case, we are taking New Release as current html tag, andinspect the immediate html tag, which is today’s Deals in the particular web page with the help of following-sibling.
which is today’s Deals in the particular web page with the help of following-sibling.

While writing a customize XPath for the following sibling to locate the element in the console window.

The console window will highlight, as you can see in the below snapshot.

console window will highlight

And, after that you can copy that XPath and paste in it your java code, and our final test script will look like this:

package testpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Following_sibling_xapth_axes {
public static void main(String[] args) throws InterruptedException {
//set the system property System.setProperty("webdriver.chrome.driver","C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe");
//creating the object for Chrome driver   
WebDriver driver = new ChromeDriver();
//Navigate to amazon home page
driver.get("https://www.amazon.in");
driver.manage().window().maximize();
//using following-sibling XPath axes
driver.findElement(By.xpath("//a[contains(text(),'New Releases')]/following-sibling::a[1]")).click();
Thread.sleep(3000); 
System.out.println("following-sibiling axes is clicked");
//closing the browser 
driver.close();
}
} 
  • To run the above code in the Eclipse, we have to right-click on the code and then select Run As ? Java Application.
  • The above test script will launch the Google Chrome browser and automate all the test scenarios.
script will launch the Google Chrome browser and automate all the test scenarios.

Preceding Sibling:

Whenever the element is not able to identify using one or more attribute, parent html tag, and grandparent html tag, in such cases, we can take the help of preceding sibling keyword to identify the element uniquely.

Preceding-sibling keyword will be used to traverse from the current html tag to the previous html tag.

Syntax for preceding sibling:

//current html tag[@attribute =’value’]/preceding-sibling::previous tag[@attribute =’value’]

Let us consider the above example, where we will try to identify a web element using preceding sibling present in UI.

Preceding Sibling

Follow the steps given below:

  • Open URL: https://www.amazon.in in your Google Chrome browser.
  • Then go to the navigation bar, and take one element as current html tag,
  • In our case, we are taking New Release as current html tag, andinspect the previous html tag, which is the Gift Idea in this particular example of the web page with the help of preceding-sibling.

While writing a customize XPath for the preceding sibling to locate the element in the console window.

The console window will highlight, as you can see in the below snapshot.

customize XPath for the preceding sibling

And, after that you can copy that XPath and paste in it your java code, and our final test script will look like this:

package testpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class preceding_sibling_Xpath_axes {
public static void main(String[] args) throws InterruptedException {
//set the system property            
System.setProperty("webdriver.chrome.driver","C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe"); 
//creating the object for Chrome driver  
WebDriver driver = new ChromeDriver();
//Navigate to google search page
driver.get("https://www.amazon.in");
driver.manage().window().maximize(); 
//using preceding-sibling XPath axes
driver.findElement(By.xpath("//a[contains(text(),'New Releases')]/preceding-sibling::a[1]")).click();
Thread.sleep(3000); 
System.out.println("preceding-sibling axes is clicked");
//closing the browser 
driver.close();
}
} 
  • To run the above code in the Eclipse, we have to right-click on the code and then select Run As ? Java Application.
  • The above test script will launch the Google Chrome browser and automate all the test scenarios.
script will launch the Google Chrome browser

Ancestor:

The Ancestor XPath axes are used to identify the parent and grandparent tag of the current html tag in the web pages.

Syntax for descendent XPath axes:

//*[attribute=’value’]/ancestor::tagname

Let us take one real-time example where we will try to identify the ancestor web element.

For the ancestor XPath axes, we are finding the second field, which is the surname text box from the female radio button present in the Facebook registration form application.

To locate the second field,

First, we will identify the female radio button, and then go for the ancestor, and finally locate the XPath for the web element.

 Follow the below steps:

inspect the female radio button.

And write the customize XPath for it.

write the customize XPath for it.

Now, we are going to locate the ancestor from the above XPath, and there are showing 16 matching nodes.

showing 16 matching nodes.

Now, we are checking that under which div our requirement is matched, and complete our XPath.

checking that under which div our requirement is matched

And, after that you can copy that XPath and paste in it your java code, and our final test script look like this:

package testpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Ancestor_xpath_Axes {
public static void main(String[] args) throws InterruptedException {
//set the system property            
System.setProperty("webdriver.chrome.driver","C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe"); 
//creating the object for Chrome driver  
WebDriver driver = new ChromeDriver();
//Navigate to Facebook search page
driver.get("https://www.facebook.com");
driver.manage().window().maximize(); 
//using ancestor XPath axes                                  driver.findElement(By.xpath("//input[@id='u_0_9']/ancestor::div[2]//input[@name='lastname']")).click();
Thread.sleep(3000); 
System.out.println("Ancestor axes is clicked");
//closing the browser 
driver.close();
}
} 
  • To run the above code in Eclipse, we have to right-click on the code and then select Run As ? Java Application.
  • The above test script will launch the Google Chrome browser and automate all the test scenarios.
above test script will launch the Google Chrome

Descendant:

The next XPath axes are descendant which are used to identify the child and grandchild tag of the current html tag in the web pages.

Syntax for descendant XPath axes:

//*[attribute=’value’]/descendant::tagname

Let us take one example where we will try to identify the Coupons link with the help of descendant XPath axes.

Follow the below steps to take the overview of the descendant axes:

  • Open https://www.amazon.in URL in your Google Chrome browser.
  • And inspect the navigation bar of the Amazon home page,
inspect the navigation bar of the Amazon home page

Let’s identify the first div tag of the page using relative XPath: //div[@id='nav-xshop-container'

And, execute the above XPath in the chropath, and notice the div tag is located on the developer tool console window.

 notice the div tag is located on the developer tool console window.

After that, customize this XPath expression, and write the XPath for coupons with the help of descendant axes.

After writing the XPath into the console, and it is showing only one matching node,

It means that we write the correct XPath for the particular web element, which is shown in below snapsort:

XPath into the console

And, after that you can copy that XPath and paste in it your java code and our final test script look like this:

package testpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class descendant_xpath_axes {
public static void main(String[] args) throws InterruptedException {
//set the system property            
System.setProperty("webdriver.chrome.driver","C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe"); 
//creating the object for Chrome driver  
WebDriver driver = new ChromeDriver();
//Navigate to Amazon home page
driver.get("https://www.amazon.in");
driver.manage().window().maximize();                                              
//using descendant XPath axes
driver.findElement(By.xpath("//div[@id='nav-xshop-container']/descendant::a[5]")).click();
Thread.sleep(3000); 
System.out.println("descendant axes is clicked");
//closing the browser 
driver.close();
}
} 

Note: We can also change the index value according to the requirement like [1], [2], and [4]

  • To run the above code in the Eclipse, we have to right-click on the code and then select Run As ? Java Application.
  • The above test script will launch the Google Chrome browser and automate all the test scenarios.
 your java code and our final test script look like

 

 


Related Topics

Mouse and Keyboard Controls in Selenium WebDriver

Mouse and Keyboard Controls in Selenium WebDriver In this tutorial, we will learn about how to handle mouse and keyboard event using action class in selenium WebDriver. Action Class in Selenium WebDriver: Action...

13 minutes read.

WebDriver-Tag Name Locator

Selenium WebDriver-Tag Name Locator The tag name locator is used along with the findElements() method to identify multiple similar elements in the UI. It is used to locate the web element with...

3 minutes read.

Data-Capture Controls Methods in Selenium WebDriver

Data-Capture Controls Methods: The data-capture controls are used to determine the text, size, location, and the CSS value of the web element, etc. Some of the most commonly used data-capture controls are...

7 minutes read.

Verification Controls Methods in Selenium WebDriver

Verification Controls Methods  The verification controls methods are used to check whether the web element is displayed, enabled, and selected. Some of the most commonly used verification controls are as follow: Verification controls Description Return...

4 minutes read.

Drag and Drop Event Handling

Drag and Drop Event Handling: In this tutorial, we will learn how to handle the drag and drop event in selenium WebDriver using the action class. Before going further in this tutorial...

6 minutes read.

CSS Selector: Sub String in Selenium IDE

CSS Selector: Sub String CSS Selector Sub-string is used to match a partial string to locate a particular web element. There are three ways to match a substring using a CSS Selector. Match...

2 minutes read.

Selenium WebDriver Tutorial

Seleniun WebDriver Introduction Selenium WebDriver is the essential tool of Selenium Tool Suite because it can directly communicate the browser without any server.  Selenium WebDriver is a browser automation framework that accepts...

3 minutes read.

Name Locator in Selenium IDE

NAME LOCATOR: It is used to find the name in the web element. If we have multiple web elements of the same name, then the first matched element would be returned. Open the...

3 minutes read.

WebDriver Interface | Class Diagram

WebDriver interface/class diagram: The interface of WebDriver includes the following stages, which is as follows: Search Context Web driver Remote web driver Driver class Search Context  The first interface of the web driver class is search Context,...

2 minutes read.

Selenium Wait

In this tutorial, we will understand how we can avoid synchronization issues while executing the test script in a selenium WebDriver. Before going further in this tutorial, first, we will understand that...

10 minutes read.

Selenium-IDE Locators

Selenium-IDE Locators: Before we start learning about Selenium –IDE Locators, firstly we have to learn about UI elements. UI Elements -UI elements are the elements which are displayed on the web application....

1 minute read.

Selenium Testing

Selenium is a functional testing tool and compatible with non–functional testing tool. Selenium is free and not required any licensing. There are specified types of testing that can be automated using Selenium:-   Functional Testing: Checking every component...

3 minutes read.

Selenium WebDriver – Web Element Controls

Selenium WebDriver – Web Element Controls A web element is an interface that is implemented by a remote web element class. The Web element control can be used to perform the data...

1 minute read.

Selenium-IDE Installation in Chrome

Selenium-IDE Installation Since Selenium IDE is designed only for Firefox and Chrome plug-in, we are assuming that you have already installed the Google Chrome browser in your system. However, you can...

1 minute read.

WebDriver-Link Text locator

WebDriver-Link Text locator The Link text locator is used to identify only web link elements based on the value of the link. The Link text locators cannot be used for other than...

4 minutes read.

CSS Selector Locator in Selenium IDE

It used to identify the web element based on HTML tag, id, class, and attributes. To describe the look and formatting of a document written in the markup language are known...

1 minute read.

Selenium-IDE Installation in Mozilla Firefox

A beginners guides to Selenium-IDE Installation Since Selenium IDE is designed only for Firefox and Chrome plug-in, we are assuming that you have already installed the Mozilla Firefox browser in your...

1 minute read.

Selenium WebDriver – Window handling

Selenium WebDriver – Window handling In this tutorial, we will understand how we can handle the window, multiple windows, and pop-ups in selenium WebDriver. We have different types of window, and pop-ups...

7 minutes read.

CSS Selector: Attribute in Selenium IDE

CSS Selector: Attribute Locating the attribute of a CSS selector, we would access the Password textbox, which is present in the login form of Facebook. The Password textbox has a type attribute...

1 minute read.

Selenium-IDE Login test

Login test for Selenium-IDE Now we are learning, how to create a Login/sign-up Test case in Selenium IDE. For our testing purpose, we are going to test the login page provided by...

2 minutes read.