Writing Effective XPaths in Selenium

Writing Effective XPaths in Selenium

Table of Contents

Finding a web element has always been the most important step in creating an automation script. Any automation test development process has always had trouble finding the right, most useful locators. It has forced QA engineers to consider locators other than id, name, class, link, or tagName. XPaths in Selenium has consistently been one of the QAs’ preferred locators, especially for finding dynamic items. Selenium’s implementation of XPaths in Selenium offers a variety of axes and functions that make it easier to create effective XPaths for web elements and specify a distinctive location for each web element.  Check out the Selenium training online to learn more about XPaths.

What are XPath Functions in XPaths in Selenium?

It can be difficult to find a specific web element using general criteria like name, class, etc. when working in a dynamic web environment. Similar characteristics among several elements, such as names or class names, may exist. Even the straightforward XPath techniques we covered in the previous chapter could not be particularly effective in this situation because a straightforward XPath might return several elements. XPath in Selenium provides XPath functions that can construct efficient XPaths to uniquely identify elements in order to avoid such scenarios. Let’s examine the several Selenium services that XPath offers to better understand how it aids in precisely discovering web elements.

1.Xpath Contains() function

One of the methods used when building an XPaths in Selenium expression is XPath Contains(). If a portion of the value of any attribute changes dynamically, we can use it. By using its partial value, it can recognize any attribute. 

The XPath includes() method’s syntax is as follows:

//tag_name[contains(@attribute,’value_of_attribute’)]

where the contains() method takes two inputs:

  • the tag’s attribute that needs to be verified in order to find the web element.
  • the Attribute’s Partial Value, which it is expected to have.

The “id” attribute has the value “username” in it. We can use a portion of the value and use it with contains() to identify the element rather than using the entire value “username”.

As a result, the following XPath can locate the element:

//input[contains(@id, “userN”)]

Here, “userN” has been used as a partial value. Any portion of the property value that seems relevant can be used.

The Email textbox’s XPath can likewise be written similarly. This element’s DOM is as follows:

Writing Effective XPaths in Selenium

<input autocomplete=”off” placeholder=”[email protected]” type=”email” id=”userEmail” class=”mr-sm-2 form-control”>

Any of the properties can be used to identify an element. Let’s use a placeholder as an example to illustrate how an attribute can be used to specify an element. 

By applying the partial validation of the properties, we may therefore discover any web element. By keeping a portion of the attribute static, this method is especially useful when the web element’s attributes vary as the page loads.

2.XPath Starts-with() function

As implied by its name, the XPaths in Selenium starts-with() method locates the element whose attribute value begins with the specified character or character sequence. Using this function when working with dynamic web pages is quite helpful. Consider an element whose attribute value changes each time the page loads or a page is used. These dynamic parts often begin with a few common characters and then contain random dynamic sentences. This can also identify static items in addition to those with the dynamic attribute.

Syntax of the starts-with() method for XPath is:

//tag_name[starts-with(@attribute,’Part_of_Attribute_value’)]

where the two parameters that the starts-with() function accepts are:

the tag’s attribute that needs to be verified in order to find the web element.

The Attribute’s Partial Value is what we anticipate it to be when it first begins.

3.XPath Text() function

For the purpose of identifying the web element on the webpage, this function uses the element’s text. If your element has text, such as labels, which always have static content, this function is quite helpful.

The XPath text() function has the following syntax:

//tag_name[text()=’Text of the element’]

where the text() method compares the value to the string supplied on the right side and returns the text of the web element indicated by the tag_name.

AND & OR operator XPaths in Selenium

Any element from a webpage can be efficiently identified with XPaths in Selenium by combining two separate requirements or attributes with the “and” operator. Using the “and” operator, for instance, we can combine two properties, a and b, to specifically identify an element on the webpage.

The “and” operator is used in the following syntax:

//tag_name[@name = ‘Name value’ and @id = ‘ID value’]

We are utilising the name and id attributes in this case, but you can use any attribute that is needed to identify the element specifically.

Writing Effective XPaths in Selenium

When using the “or” operator, an element can be found depending on any of the conditions it has been separated into. We primarily employ it based on specific run-time circumstances. A condition with the word “or” ensures that the element can be located with any one of those criteria because the element’s attributes can contain any of the values.

What are the XPath Axes in Selenium?

According to what we’ve read, Selenium’s XPath uses both absolute and relative paths to find web elements. Additionally, a hierarchical structure governs the relationships between each web element in the XML DOM. The “XPath Axis” features that are offered by XPaths in Selenium find specific nodes in the DOM structure by utilising relationships between different nodes.

Conclusion In conclusion, XPaths in Selenium is one of the most well-liked Selenium locator methods because it can even discover elements in a dynamic web environment using its capabilities and axis. A web element’s ID, class, name, substring, inner Text, or any combination of these using the “AND” or “OR” operators can be used to build an XPaths in Selenium. Check our Selenium online training.

5 Responses

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Enroll Free demo class