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

how to find xpath in selenium technical guide

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.

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.
const min = parseInt(prompt("Enter a min value: "));

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) {
        // 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:

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

FAQs

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.

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

Introducing Katalon AI Test Runner. Watch it run manual test cases live, with evidence for every pass and fail.

Live webinar
9:00 to 10:00 AM EST · Live online
Alex MartinsVP of Strategy
Gia EspositoSenior Solutions Engineer
Save your seat