Skip to main content

XPath tutorial for Selenium

XPath is designed to allow the navigation of XML documents,with the purpose of selecting individual elements, attributes, or some other part of an XML document for specific processing.
What is XML?
The Extensible Markup Language (XML) is the context in which the XML Path Language, XPath, exists.
XML provides a standard syntax for the markup of data and documents.
XML documents contain one or more elements. If an element contains content,whether other elements or text, then it must have a start tag and an end tag. The text contained between the start tag and the end tag is the element’s content.
<Element> //Start tag
Element content goes here.//Element Content
</Element>//End Tag
An element may have one or more attributes, which will provide additional information
about the element type or its content.

Below is the sample XML:
<?xml version='1.0'?>
<Catalog>
<Book>
<Title>XML Tutorial</Title>
<Author>Selenium Easy</Author>
</Book>
</Catalog>
It can also be written as:
<?xml version='1.0'?>
<Catalog>
<Book Title="XML Tutorial" Author="Selenium Easy">
</Book>
</Catalog>
XPath can be viewed as a way to navigate round XML documents. Thus XPath has similarities to a set of street directions.
When you need to search for a address, you should know what is your starting point to reach your destination.
In XPath the starting point is called the context node.
Absolute XPath
Absolute XPath starts with the root node or a forward slash (/).
The advantage of using absolute is, it identifies the element very fast.
Disadvantage here is, if any thing goes wrong or some other tag added in between, then this path will no longer works.
Example:
If the Path we defined as
1. html/head/body/table/tbody/tr/th
If there is a tag that has added between body and table as below
2. html/head/body/form/table/tbody/tr/th
The first path will not work as 'form' tag added in between
Relative Xpath
A relative xpath is one where the path starts from the node of your choise - it doesn't need to start from the root node.
It starts with Double forward slash(//)
Syntax:
//table/tbody/tr/th
Advantage of using relative xpath is, you don't need to mention the long xpath, you can start from the middle or in between.
Disadvantage here is, it will take more time in identifying the element as we specify the partial path not (exact path).
If there are multiple elements for the same path, it will select the first element that is identified
XPath Axes :
XPath has a total of 13 different axes, which we will look at in this section. An XPath axis tells the XPath processor which “direction” to head in as it navigates around the hierarchical tree of nodes.
Xpath axis NameDescription
selfWhich contains only the context node
ancestorcontains the ancestors of the context node, that is, the parent of the context node, its parent, etc., if it has one.
ancestor-or-selfcontains the context node and its ancestors
attributecontains all the attribute nodes, if any, of the context node
childcontains the children of the context node
descendantcontains the children of the context node, the children of those children, etc.
descendant-or-selfcontains the context node and its descendants
followingcontains all nodes which occur after the context node, in document order
following-siblingSelects all siblings after the current node
namespacecontains all the namespace nodes, if any, of the context node
parentContains the parent of the context node if it has one
precedingcontains all nodes which occur before the context node, in document order
preceding-siblingcontains the preceding siblings of the context node


The below are the Axes that are very useful
1. Child Axes
The child axis defines the children of the context node.
Child::*
Syntax:
//child::table 
The first location step selects the child element node of the root node, which represents the element root
element in the source document.
The child axis is the default axis, so it need not be explicitly expressed in the abbreviated.
It can be simply re-written as:
//table/tbody
//child::*/child::td[position()>1]
The position ( ) function, evaluates the context position of the context node within the context size. The position ( ) function is applied to the selected nodes in document order. It will select the second td in a table
It will select all the nodes that are Child nodes of table.
Please find the below screen shot for example.
Child Axes
2. Parent Axes
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.
Syntax:
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']/..
Below is the image that shows you to identify using above example.
Parent Axes
3. Following Axes
“Following axis contains all nodes in the same document as the context node that are after the context node in document order.
Syntax:
The below syntax selects the immediate node following the specified node input[@id='email']
//input[@id='email']/following::*
Below is the image that shows you to identify using above example.
It will identify the immediate node which start after the current node.
Following Axes
The below syntax selects the immediate node of tag 'tr' with the specified node input[@id='email']
//input[@id='email']/following::tr
Below is the image that shows you to identify using above example.
It will identify the immediate node which start after the current node.
Following Axes
4. Following Sibling Axes
The following-sibling axis selects those nodes that are siblings of the context node (that
is, the context node and its sibling nodes share a parent node) and which occur later in
document order than the context node.
Syntax:
//select[@id='month']/following-sibling::*
//select[@id='month']/following-sibling::select/
Please check the below image for the above syntax executed
Following Sibling Axes
5. Preceding Axes
The preceding axis contains all nodes in the same document as the context node that are before the context node in document order.
Syntax:
//input[@id='pass']/preceding::tr
Below screen shot shows how the preceding axes selects nodes that appear before the current node in the document, except ancestors, attribute nodes and namespace .
Preceding Axes
6. Preceding Sibling Axes
The preceding-sibling axis selects those nodes which are siblings of the context node (that is, the context node and its sibling nodes share a parent node) and which occur earlier in document order than the context node.
Syntax:
//select[@id='day']/preceding-sibling::select/
//select[@id='day']/preceding-sibling::*
The below image shows how the preceding sibling axes selects siblings before the current node
Preceding Sibling Axes

Comments

Popular posts from this blog

API Testing With Selenium WebDriver

REST API Testing Framework We will be creating a simple Rest Testing Framework in Java and JUnit that could be used for any testing scenarios. Rest Testing Framework Overview The framework should be able to execute the basic REST operations (GET, POST, PUT, PATCH, DELETE) and perform the validations on the code, message, headers and body of the response. The completed code can be accessed from my  GitHub account   from where you can collect and make modifications based on your requirements. Design We will be having three classes for the framework (in package com.axatrikx.controller ) RestExecutor  : Performs the HTTP operations using Apache Http Client RestResponse  : A javabean class to hold our response values (code, message, headers, body) RestValidator  : Validates the response with the expected values The package  com.axatrikx.test  holds the test scripts. Note: I will be using ‘ json-server ‘ to fake the REST API. Its a real handy tool to r

ExtentReports in Selenium Webdriver

ExtentReports in Selenium Webdriver What is ExtentReport? ExtentReports  is a HTML reporting library for Selenium WebDriver for Java which is extremely easy to use and creates beautiful execution reports. It shows test and step summary, test steps and status in a toggle view for quick analysis Download Download the jar below: Download ExtentReports 1.4 (1623)    Snapshot of Extent report After Executing the Script   Program Steps:  We are going to write three different testcases. Pass Warning Fail TestCase with Pass Result Navigate to http://www.guvi.in Click on Sign-in Enter the credientials Check the URL is correct or not after login   TestCase with Warning Result Verify with the Wrong URL (static String Afterloginfail="http://www.guvi.in/ ")    TestCase with fail Result Click on Menu Select Tech Challenges Verify With wrong URL. Source Code: import  java.io.File; import  java.io.IOException; import   java.sql

How to Compare Two Images in Selenium

How to Compare Two Images in Selenium Some time in testing we have to do image comparision as verification point. So, in this blog I expalin how to compare two images in selenium. I have created one function for compare two images. you can use directly into your framework. first of all you have to import below packages into your code. import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; Now, you can use below function for comparison of two images. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 public boolean mCompareImages ( BufferedImage img1 , BufferedImage img2 ) { boolean bCompareImage = true ; try { if ( img1 . getWidth () == img2 . getWidth () && img1 . getHeight () == img2 . getHeight ()) { for ( int i = 0 ; i < img1 . getWidth (); i ++) { for ( int j = 0 ; j < img1 . getHeight (); j ++) { if ( img1 . get