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:
Make sure you change C:/path/to/chromedriver.exe to wherever your ChromeDriver is located. What the code does is:
- Import necessary libraries
- Launch the W3Schools' HTML Forms page
- Find the "Submit" button using XPath attribute
- Click the button
- 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:
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.
