A Complete Tutorial for XPath in Selenium
Finding web elements is a crucial component of web application automation and a key activity in the creation of automation scripts. HTML components can be recognised thanks to specific properties like id, class, name, and others. However, it is possible that some web elements may not be associated with any special properties. In this scenario, XPath in Selenium Automation Testing serves as the technique for making it easier to locate web items. It serves as a query language that aids in removing entities from the Document Object Model (DOM).
In this blog post on XPath in Selenium, we'll aim to look at
some of the most important parts of XPath Selenium, including what it is,
distinct types of XPath, and the various XPath expressions that may be used to
find various types of elements.
Describe XPath
XPath types
Functions for XPath
XPath Axes Procedures
Conclusion
Describe XPath
XML Path, also known as XPath in Selenium, is a method for
navigating a webpage's HTML structure as well as querying XML data. With the
use of an XPath query or script written in the language, web items on a webpage
can be located and identified using an XML Path Expression.
Selenium's generic locators may occasionally be insufficient
for finding all DOM elements in an HTML document. Here's where XPath Selenium
comes into play. It helps to provide for dynamic element search, which gives
one the much-needed flexibility to customise a location to their demands.
XPath syntax
XPath=/tagname[@Attribute=’Value’]
To choose the current node, use /
• Tagname: The name of a certain node's tag
• @: To choose an attribute
• Attribute: This is the node's attribute name.
• Value: This is the node's value.
Different XPath
Locator Types
• ID: uses the element's ID to locate the element
• Name: uses the element's name to locate the element
• Classname: uses the element's classname to locate the
element
• XPath: locates dynamic elements and navigates between
various webpage elements.
• Link text: uses a text's link to locate an element
Locates elements without a class, ID, or name using CSS Path
XPath types
Infinite XPath
It is one of the straightforward methods for locating an
element because the XPath expression is created by using a selection from the
root node. In order to find the relevant element, the XPath in this instance
starts with a single slash ('/') and moves from the root to the entire Document
Object Model (DOM).
This method has a significant flaw in that any modification
to the element's path would lead to an unsuccessful XPath expression.
For illustration:
http://body.form.input/html [5]
The first form tag in the page body would be the starting
point for this kind of search, which would then choose the fifth form input
field.
Referential XPath
The expression begins in the middle of the HTML DOM
structure when using Relative XPath in Selenium. The expression is
distinguished by having a double slash (//) at the beginning, which designates
the current node. The given tagname and string value serve as the starting
point for the search, which aids in finding elements elsewhere on the website.
Comparing this method to Absolute XPath Selenium, its main
benefits include being simpler to use, more compact, and less likely to break.
For illustration:
XPath relative: /input[@name='email']
We will start our search from the current node that has the
tagname as input, whose attribute is name, and whose value is email, according
to the provided XPath expression in Selenium.
Functions for XPath
On sometimes, elements on a webpage may share properties,
making it challenging to distinguish between them. The similarity of element
names and attributes makes it difficult for Selenium to distinguish between any
one specific element. XPath Functions come to our aid in these circumstances.
XPath Function
Contains ()
When the value of any attribute changes dynamically, you can
effectively generate an XPath expression using this function. In this case,
using the provided partial text, the contain function aids in detecting a
specific web element.
The attribute of the tag must successfully locate the
particular web element in order for the function to work, and the attribute's
value must be a partial value that the attribute contains.
For illustration:
/tagname[contains(@attribute,’value of attribute’)]
XPath Function
Starts-with()
When the value of an attribute changes on page refresh or as
a result of any other dynamic operation on the webpage, this function can be
used to successfully generate an XPath expression. The element whose value may
have changed is then found by matching the initial text of the attribute using
the starts-with function. The function can also be used to locate elements
whose attribute values are static or whose attribute names begin with a
particular character or string of characters.
The attribute of the tag's value must be a partial value of
the attribute with which the attribute is expected to start in order for the
function to succeed in locating the specified web element.
For illustration:
/tagname[starts-with(@attribute,’Part of Attribute Value’)]
Text() XPath Function
Based on the text of the element, elements are located on a
webpage. The text function looks for an exact match to discover the text
element among the list of text nodes. Additionally, the element that is being
sought after must be present in string form for this function to work properly.
When an element is identified by its tagname and compared to
the value of an attribute, the text() function can be utilised to supply us
with the element's text.
For illustration:
'Text of the element' /tagname
XPath Axes Procedures
The XML DOM's elements are set up in a certain order. They
can be found in a hierarchical structure and are often found using either of
the two XPath types: absolute or relative. These characteristics, referred to
the XPath Axis, are provided by XPath in Selenium in order for this operation
to succeed.
In order to find the relative nodes, the XPath Axes Methods
are utilised to determine relations with the current node. We will examine a
few of the typical XPath Axes Methods in this section.
Axis |
Description |
Ancestor |
It helps in locating all the
ancestor elements of the current node (grandparent, parent, root node and so
on) |
Ancestor-or-self |
It helps in locating the current
node along with the ancestors |
Child |
It helps in locating all children
elements of the current node |
Attribute |
It helps specify the attributes
of the current node |
Following |
It helps in locating all the
elements which come after the current node |
Descendant |
It helps in locating the children
of the current node, up to the leaf node |
Descendant-or-self |
It helps in locating the current
node, along with its descendants |
Self |
It helps in locating the current
node which is designated as self |
Parent |
It helps in locating the parent
of the current node |
Conclusion
I'm confident that at the conclusion of this article you
will have a solid understanding of how Selenium's XPath function. It is clear
that even while the necessity to find web components during Selenium Automated Testing,
especially cross-browser testing, may be a major problem, the traditional
approaches might not always function for various reasons. Selenium's XPath
feature offers a workaround for locating web items in such circumstances.
However, it is crucial to understand the applicability of the various XPath
Selenium classes and functions in various contexts, depending on functional
requirements and the complexity of the DOM structure. You may write scripts
that are clean, efficient, and robust using this advice.
One of the top software testing tools of preference for
software development engineers in test is undoubtedly Selenium (SDETs). Enroll
in our SDET Automation Course right away if you want to develop the abilities
required to become an SDET and land this highly sought-after career.
Keywords |
Frequency |
XPath in Selenium – PK |
10 |
XPath Selenium |
6 |
What is XPath |
2 |
Types of XPath |
5 |
XPath Axes Methods |
5 |
Comments
Post a Comment