Xpath in Selenium - Selenium Testing Tutorial

Xpath in Selenium

  • XML path in selenium is commonly known as Xpath, is a query language for XML Documents.
  • Xpath is made of path expressions governed by some conditions.
  • In selenium xpath is used to identify and locate any element present on a web page.
  • Xpath is very helpful in handling dynamic web elements whose attribute changes.
  • Moreover, it allows us to write XML document navigation flow for locating web elements.

Syntax of XPath

  • XPath finds any element within a webpage by using DOM. So, its syntax is also made up of DOM attributes and tags, as shown below:
XPath = //tag_name[@Attribute_name = “Value of attribute”]
Syntax of Xpath

Syntax of XPath

  • // generally represents the start of Xpath syntax means it will begin with the current node defined by the tag name.
  • Next part after // is tagname, it denotes the HTML tag name of the element. 
  • Anything that is present inside the node is enclosed in square brackets.
  • Additionally, the “@” sign selects the attribute.
  • Name attribute of the node is represented by Attribute Name. 
  • Node attribute value is represented by value of the attribute.

Locating web elements using XPath in Selenium

Syntax Element Details Example Example Details
Single / It selects the node from the root.
In other words, if you want to select
the first available node, this expression
is used.
/htmlIt will look for the HTML element at the start of the document.
Double slash "//" It selects any element in the DOM that
matches the selection. Additionally, it
doesn't have to be the exact next node
and can be present
anywhere in the DOM.
//inputIt will select the input node present anywhere in the DOM.
Address sign "@" It selects a particular attribute
of the node.
//@textIt will select all the elements which have text attribute.
Dot "." It selects the current node.//h3/.It will give the currently selected node, i.e., h3.
Double dot ".." It selects the parent of the
current node.
//div/input/..It will select the parent of the current node. The current node is input so that it will select the parent, i.e., "div".
Asterisk "*” It selects any element present
in the node
//div/*This matches with any of the child nodes of the "div".
Address and Asterisk "@"* It selects any attribute of the
given node
//div[@*]It matches any of the div nodes that contain at least one attribute of any type.
Pipe "|" This expression is used to select a
different path.

Different types of XPaths in Selenium

  • Selenium uses two strategies to locate elements using XPaths
    • Locating a web element using an Absolute XPath.
    • Locating a web element using Relative XPath.
Types of xpath

Absolute Xpath in Selenium

  • Absolute xpath is direct way of finding the element.
  • Absolute xpath starts from the root node and goes all the way to the required node one at a time in HTML.
  • Ex: To find the absolute path of <img> node, we will have to start from the root node(<htm> tag) and go all the way to the required node in HTML.
  • Absolute Xpath for the above example will be:
    • /html/body/div/header/a/img
  • The main disadvantage of this xpath is that if there is any change in the elements on the webpage, the html will change and the xpath will also change.
  • This makes it difficult to identify the web element which results in failure of test scripts. So absolute xpath is not recommended for identifying or locating web elements on the web page.

Relative Xpath

  • Relative xpath does not start from the root node but relative xpath starts with any node inside the DOM in HTML.
  • It starts with double forward slash(//).
  • We know that absolute xpath starts from the root node i.e.html tag and go down one node to another till the required node. It traces the whole path and finds the exact position of the element in the page but if there is any change in node, the absolute xpath will break and will not find the exact match of the node. This is the time where Relative Xpath is useful.
  • The relative xpath uses the current position of the node on the page.For ex to write a relative xpath for the same image element that was used in absolute xpath
  • /html/body/div/header/a/img
  • We dont need to write the entire path for finding the img tag. Instead we can start from the image tag using double slash.
  • //img[@src = "/images/ jpg"]
  • As shown above the relative xpath can start from any node in html and using relative xpath will overcome the disadvantage of absolute xpath and even if any element changes in HTML relative xpath will not be affected.
Finding xpath nodes

Related Searches to Xpath in Selenium - Selenium Testing Tutorial