How to Find XPath in Selenium: Step-by-Step Examples (Java & Python)

how to find xpath in selenium technical guide

How to find and use XPath in Selenium, with step-by-step Java and Python examples, plus how Katalon Studio's Smart XPath and Spy Web let you capture reliable XPath locators without writing them by hand.

Key takeaways
The 30-second version of this article
XPath locates web elements by path expressions, which helps when CSS selectors fall short in dynamic or deeply nested pages.
The basic syntax is //tagname[@attribute='value'], for example //input[@type='submit'] for a submit button.
In Selenium you pass an XPath to By.xpath() in Java or By.XPATH in Python to find and act on an element.
Katalon Studio's Spy Web captures an element with a Smart XPath locator and saves it to the Object Repository, so you don't have to write XPath by hand.

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: //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.

For example, this expression finds every input element whose type is submit:

//input[@type='submit']

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:

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) {
        // Selenium 4.6+ finds or downloads the matching ChromeDriver automatically
        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();
        }
    }
}

With Selenium 4.6 or later, Selenium Manager handles the browser driver for you, so there's no driver path to set. 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:

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

# Selenium 4.6+ finds or downloads the matching ChromeDriver automatically
driver = webdriver.Chrome()

try:
    # 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()
finally:
    # 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:

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

WebUI.openBrowser('https://katalon-demo-cura.herokuapp.com/')
WebUI.click(findTestObject('Object Repository/Page_CURA Healthcare Service/a_Make Appointment'))
WebUI.closeBrowser()

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 stable 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

FAQs

Frequently asked questions

Trinh Huynh
Trinh HuynhTechnical 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.

Connect on LinkedIn →
Get started

Test everything. Ship with confidence.

From generating test cases to spotting failures, Katalon's AI works across your entire pipeline, automatically.

Live webinar

The Next Evolution of Manual Testing: Introducing Katalon AI Test Runner

Learn how to grow your QA team's capacity in the age of AI, then watch Katalon AI Test Runner turn manual test cases into automated runs, live.

2:00 to 3:00 PM IST
Almas AhmedSenior Account Manager
Sumit WankhedeSolutions Engineer Manager
Register now