How to Find XPath in Selenium? A Complete Guide
Learn with AI
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:
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:
- 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:
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.
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:
-
Open Spy Web and enter your URL.
-
Hover over the target element, right-click, and select “Capture.”
-
Katalon will show the element’s Smart XPath in the “Selected Locator” box—often combining multiple attributes for stability.
-
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.
|