• Articles

What is XPath in Selenium?

In this blog, we will explore XPath in the context of Selenium, starting from the basics and gradually delving into more advanced concepts. 

Table of Contents

Check out this video on Selenium Tutorial For Beginners to learn all its concepts:

XPath in Selenium Explained

XPath, which stands for XML Path Language, is a query language used to navigate through XML and HTML documents. It provides a way to traverse the structure of an XML or HTML document and select specific elements based on their attributes, text content, or position in the document hierarchy. XPath is widely used in Selenium automation testing to locate elements on web pages and interact with them.

XPath Syntax and Expressions

XPath expressions are used to specify the location of elements within an XML or HTML document. These expressions follow a specific syntax that consists of various components. Here are the key elements of XPath expressions:

Key Elements Of Xpath Expressions
  • Nodes: In XPath, everything is considered a node, including elements, attributes, text nodes, and more. Nodes can be selected and filtered based on their type and properties.
  • Axes: Axes define the relationships between nodes in the document hierarchy. Commonly used axes include child, parent, sibling, and descendant axes. These axes help to navigate through the document and select specific nodes.
  • Predicates: Predicates are used to further filter nodes based on specific conditions. They are enclosed in square brackets and can contain logical and comparison operators, as well as functions.
  • Functions: XPath provides a set of built-in functions that can be used to perform various operations on nodes and values. And it can manipulate strings, perform mathematical calculations, extract substrings, and much more.

Do you want to become a professional in the field of Selenium!! We recommend you enroll in our Selenium Certification training program.

Absolute XPath in Selenium

Absolute XPath provides the complete path from the root element to the desired element. It starts with a single forward slash ("/") and includes the names of all the parent elements leading to the target element. Absolute XPath expressions are generally longer and more prone to breaking if the structure of the document changes.

To find the absolute XPath of an element, you can inspect the element in the browser’s developer tools and copy the XPath value. However, it is recommended to avoid using absolute XPath in Selenium tests unless there is no other alternative, as they can be brittle and less maintainable.

Want to learn about Selenium with Python? Check out our blog on Selenium Python Tutorial for beginners.

Relative XPath in Selenium

Relative XPath expressions provide a more flexible and maintainable way to locate elements. They are written based on the current context or a nearby element, rather than the entire document structure. Relative XPath starts with a double forward slash ("//") and allows you to select elements directly or traverse through the hierarchy using axes.

Relative XPath expressions are generally shorter and more robust compared to absolute XPath. They adapt well to changes in the document structure, as long as the relative positioning of the target element remains consistent.

Get 100% Hike!

Master Most in Demand Skills Now !

Dynamic XPath in Selenium

Web applications often have dynamic content, where elements’ attributes or positions may change dynamically. Dynamic XPath is used to handle such scenarios by creating XPath expressions that can adapt to these changes. It involves using wildcard characters, functions, or other techniques to select elements based on patterns or dynamic attributes.

Some common techniques for handling dynamic elements in XPath include using the contains() function to match partial attribute values, using indexes to select elements based on their position, and using logical operators to combine multiple conditions.

Handling dynamic elements effectively is crucial for robust and reliable test automation. Dynamic XPath allows testers to write flexible and adaptable locators that can handle changes in the application under test.

Check out the list of Selenium Interview Questions to prepare for your next interview.

Types of XPath in Selenium

Types of XPath in Selenium
  • Element-Based XPath
    Element-based XPath expressions allow you to locate elements based on their tag name. This type of XPath is useful when you want to identify elements without relying on specific attributes or text. For example, to locate all the <input> elements on a web page, you can use the XPath expression //input.
  • Attribute-Based XPath
    Attribute-based XPath expressions involve locating elements based on their attributes. You can specify the attribute name and its value to uniquely identify elements. For instance, to find an <input> element with the attribute name equal to “username,” you can use the XPath expression //input[@name='username'].
  • Text-Based XPath
    Text-based XPath expressions are handy when you need to locate elements based on their text content. You can use the text() function in combination with other XPath components to search for elements with specific text. For example, to find a <button> element with the text “Submit,” you can use the XPath expression //button[text()='Submit'].
  • Position-Based XPath
    Position-based XPath expressions allow you to locate elements based on their position within the document structure. You can use predicates with indices to specify the desired position. For instance, to find the second <div> element on a page, you can use the XPath expression (//div)[2].

Also, check out the Selenium Tutorial.

Locating Elements with XPath in Selenium WebDriver

Selenium WebDriver provides a variety of methods to locate elements using XPath expressions. These methods include:

  • findElement(By.xpath()): This method allows you to locate a single element using the specified XPath expression. It returns the first matching element found in the DOM.
  • findElements(By.xpath()): This method returns a list of all elements that match the given XPath expression. It is useful when you need to find multiple elements.

Here’s an example that demonstrates locating an element using XPath in Selenium WebDriver:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class XPathExample {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        driver.get("https://example.com");
        WebElement element = driver.findElement(By.xpath("//input[@name='username']"));
        // Perform actions on the located element
        driver.quit();
    }
}

Also, check out the blog Action Class in Selenium.

Advanced XPath Techniques

By leveraging these advanced XPath techniques, you can create more precise and flexible locators, enhancing the robustness and reliability of your Selenium tests.

  • Using Functions in XPath
    XPath provides a range of functions that can be utilized to construct complex expressions. Functions like contains(), starts-with(), and concat() allow you to search for elements based on partial attribute values or combine multiple values. For instance, you can use contains() to locate an element with a partial attribute value: //input[contains(@class, 'btn')].
  • Using Logical Operators in XPath
    XPath supports logical operators such as and, or, and not. These operators enable you to combine multiple conditions within a single XPath expression. For example, you can use the or operator to locate elements that satisfy one of two conditions: //input[@type='text' or @type='email'].
  • Using Predicates in XPath
    Predicates allow you to filter elements based on certain conditions. You can use predicates to specify additional constraints on attributes, text content, or position. For instance, you can use a predicate to find the second <td> element within a specific <tr>: //tr[2]/td[2].

Also, check out the blog on how to use Selenium with Java.

Conclusion

XPath is an invaluable tool for element location and navigation in Selenium automation testing. It provides a flexible and powerful language that allows testers to identify elements based on their tag name, attributes, text content, and position within the document structure.

With its ability to precisely locate elements, handle dynamic content, and navigate complex DOM structures, XPath proves to be an essential tool in the arsenal of any automation tester. By harnessing the power of XPath, testers can write robust and efficient test scripts that contribute to the overall quality and reliability of web applications.

Drop any of your queries in our Selenium Community and start a discussion.

Course Schedule

Name Date Details
Testing Courses 11 May 2024(Sat-Sun) Weekend Batch
View Details
Testing Courses 18 May 2024(Sat-Sun) Weekend Batch
View Details
Testing Courses 25 May 2024(Sat-Sun) Weekend Batch
View Details

Testing-ad.jpg