New data from 1,500+ QA pros: The 2025 State of Software Quality Report is live
DOWNLOAD YOUR COPY
All All News Products Insights AI DevOps and CI/CD Community

How to Find XPath in Selenium? A Complete Guide

Learn how to find XPath in Selenium with this complete guide. Explore XPath types, syntax, and strategies to locate web elements accurately for automation testing.

Hero Banner
Smart Summary

Mastering XPath is crucial for reliable web element location in Selenium, especially when dealing with dynamic or complex web page structures where standard CSS selectors fall short. The approach enables precise control over element selection using path expressions, providing robust solutions for automation testing. For enhanced efficiency and simplified XPath generation, tools like Katalon Studio offer Smart XPath features that automate the capture and management of locators.

  • Leverage XPath for Robust Element Location: Employ XPath as a powerful alternative to CSS selectors for pinpointing web elements, especially in dynamic or complex page structures, ensuring reliable test automation by targeting elements via attributes and hierarchy.
  • Implement Relative XPath in Selenium: Utilize `By.xpath` with relative path expressions like `//tagname[@attribute='value']` to locate elements in Selenium, prioritizing relative XPath for its resilience against DOM structure changes over brittle absolute paths.
  • Automate XPath Discovery with Smart Tools: Streamline XPath generation and management using platforms such as Katalon Studio’s Spy Web and Smart XPath features, which automatically identify and capture robust locators, simplifying their integration into test cases.
Good response
Bad response
|
Copied
>
Read more
Blog / Insights /
How to Find XPath in Selenium? A Complete Guide

How to Find XPath in Selenium? A Complete Guide

Technical Writer, Katalon Updated on

What Is XPath and Why Use It?

XPath is a powerful way to pinpoint web elements when CSS selectors fall short, especially in dynamic or deeply nested page structures. In Selenium, knowing how to write and use XPath expressions can help you reliably locate buttons, forms, and other elements even when their attributes change. This guide will show you simple Java and Python examples for finding elements with XPath in Selenium, and then demonstrate how Katalon Studio’s Smart XPath features let you capture and use XPath without memorizing complex syntax.

XPath uses path expressions to navigate through elements, attributes, and select nodes or node-sets. The basic syntax of an XPath expression is: Xpath=//tagname[@attribute='value'].

  • tagname: The name of the HTML tag you wish to target.
  • @attribute: The attribute within the tag.
  • 'value': The specific value of the attribute.

Make sure that you have installed a Selenium library. If you haven't, you can have a look at Selenium documentation for how-to guides.

Setting Up Selenium for XPath (Java & Python)

How to find XPath in Selenium using Java

You can use XPath to locate a web element in Selenium using Java. Here’s a simple example:

Copy

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class ClickSubmitButton {
    public static void main(String[] args) {
        // Set the path to your ChromeDriver executable
        System.setProperty("webdriver.chrome.driver", "C:/path/to/chromedriver.exe");

        // Initialize ChromeDriver
        WebDriver driver = new ChromeDriver();

        try {
            // Open the webpage
            driver.get("https://www.w3schools.com/html/html_forms.asp");

            // Find the submit button using XPath
            WebElement submitButton = driver.findElement(By.xpath("//input[@type='submit']"));

            // Click the button
            submitButton.click();
        } finally {
            // Close the browser
            driver.quit();
        }
    }
}

Make sure you change C:/path/to/chromedriver.exe to wherever your ChromeDriver is located. What the code does is:

  1. Import necessary libraries
  2. Launch the W3Schools' HTML Forms page 
  3. Find the "Submit" button using XPath attribute
  4. Click the button
  5. And finally close the browser and end the session.

How to find XPath in Selenium using Python

You can perform the same procedure using Python. Here’s a quick sample code:

Copy

from selenium import webdriver
from selenium.webdriver.common.by import By

# Set the path to your chromedriver
driver = webdriver.Chrome(executable_path="C:/path/to/chromedriver.exe"

# Open a webpage
driver.get("https://www.w3schools.com/html/html_forms.asp")

# Find the submit button using XPath
submit_button = driver.find_element(By.XPATH, "//input[@type='submit']")

# Click the button
submit_button.click()

# Close the browser
driver.quit()

Simplify XPath with Katalon Studio

If you’re looking for a faster, more beginner-friendly way to find and use XPath, Katalon Studio is here to help.

Instead of manually writing complex XPath in raw Selenium, Katalon Studio makes things easier by supporting Smart XPath for better object recognition.

📄 Learn how to detect objects with XPath in Katalon Studio.

And here's how you do it with visuals:

1. Open your project and select Spy Web.

2. Enter the URL of your application and click Start. An example URL we use here is: https://katalon-demo-cura.herokuapp.com/

3. In the webpage, hover your mouse over the web element > right-click on the web element and select Capture

4. The object is captured with its XPath in the Selected Locator.

5. Click Save to add the object to the Object Repository.

6. Use it in a test case with Katalon Studio built-in keywords:

Mastering XPath in Selenium gives you precise control over element selection, but writing and maintaining raw XPath expressions can become tedious. Katalon Studio’s built-in Smart XPath support accelerates that process by automatically identifying robust locators and letting you select elements visually. Whether you prefer hand-coding in Java or Python or taking advantage of Katalon’s low-code workflow, incorporating XPath effectively will make your tests more reliable and adaptable as your application evolves.

upgrade-from-selenium-to-katalon-studio

Frequently asked questions

What is XPath and why should I use it in Selenium?

+

XPath is a language for navigating XML and HTML documents using path expressions. In Selenium, it helps you select elements based on tag names, attributes, and hierarchy. You should use XPath when IDs or CSS selectors are unavailable or unstable—XPath can target elements by multiple attributes or relative positions, making your locators more flexible.

 

How do I write a basic XPath expression for Selenium?

+

A basic XPath expression follows this syntax:

XPath=//tagname[@attribute='value']

For example, //input[@type='submit'] finds any <input> element whose type attribute equals “submit.” You can add more conditions or use functions (e.g., contains(), starts-with()) to handle dynamic attributes.

What’s the difference between absolute and relative XPath?

+

  • Absolute XPath starts from the root node (e.g., /html/body/div[2]/form/input). It breaks easily if the DOM structure shifts.

  • Relative XPath starts with // and finds elements anywhere in the document (e.g., //form[@id='loginForm']//input[@name='password']). Relative XPath is more resilient to layout changes and is the recommended approach in automated tests.

 

How can I capture XPath automatically in Katalon Studio?

+

In Katalon Studio, use the Spy Web tool:

  1. Open Spy Web and enter your URL.

  2. Hover over the target element, right-click, and select “Capture.”

  3. Katalon will show the element’s Smart XPath in the “Selected Locator” box—often combining multiple attributes for stability.

  4. Click “Save” to add it to your Object Repository, then call keywords like WebUI.click(findTestObject('Object Repository/Page/MyPage/button_Login')) in your test.

 

 

Ask ChatGPT
|
Trinh Huynh
Trinh Huynh
Technical Writer, Katalon
Trinh Huynh is an experienced technical writer in the software testing industry. She blends her extensive technical knowledge with deep understanding of the Katalon products to create guides that help testers of all levels.
on this page
Click