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

April 23, 2026 · 5 min read · Tool Comparison

Blog / Insights /
How to Find XPath in Selenium: Step-by-Step Examples (Java & amp; Python)

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

Technical Writer, Katalon Updated on

Learn with AI

Linkedin

Facebook

X (Twitter)

Mail

Learn with AI

What Is XPath and Why Use It?

XPath is a powerful way to pinpoint web elements when CSS selectors fall little, peculiarly in dynamic or deep nested page structures. In Selenium, know how to pen and use XPath face can help you dependably locate buttons, forms, and other elements yet when their attributes change. This guidebook will show you simple Java and Python examples for finding elements with XPath in Selenium, and so demonstrate how Katalon Studio ’ s Smart XPath boast let you capture and use XPath without memorizing complex syntax.

XPath uses path expressions to voyage through elements, attribute, and prime node 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 have n't, you can have a face atSelenium documentationfor how-to guides.

Setting Up Selenium for XPath (Java & amp; Python)

How to find XPath in Selenium use Java

You can use XPath to place a web component in Selenium using Java. Here ’ s a mere representative:

Copy

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

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

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

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

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; // Find the submit push utilize XPath
& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; WebElement submitButton = driver.findElement (By.xpath (`` //input [@ type='submit '] ''));

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

Make sure you modifyC: /path/to/chromedriver.exeto wherever your ChromeDriver is located. What the code do is:

  1. Import necessary libraries
  2. Launch the W3Schools ' HTML Forms page & nbsp;
  3. Find the `` Submit '' push 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 operation expend Python. Here ’ s a quick sampling code:

Copy

from selenium importee webdriver
from selenium.webdriver.common.by meaning 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 look for a faster, more beginner-friendly way to find and use XPath,Katalon Studiois here to help.

For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users.

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

📄 Learn how to

And hither 's how you do it with visuals:

1. Open your undertaking and selectSpy Web.

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

3. In the webpage, hover your shiner over the web element & gt; right-click on the webfactor and selectCapture

4. The object is captured with its XPath in theSelected Locator.

5. Click Saveto add the object to theObject Repository.

6. Use it in a tryout causa with Katalon Studio built-in keywords:

Mastering XPath in Selenium gives you precise control over element option, but write and maintaining raw XPath expressions can turn tedious. Katalon Studio ’ s built-in Smart XPath support quicken that process by automatically identifying robust locators and permit you select ingredient visually. Whether you prefer hand-coding in Java or Python or direct advantage of Katalon ’ s low-code workflow, incorporating XPath effectively will make your tests more reliable and adaptable as your covering evolves.

Frequently inquire interrogation

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

+

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

 

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 & lt; input & gt;element whosetypeattribute compeer “ submit. ” You can add more conditions or use purpose (e.g.,contains (), starts-with ()) to handle dynamic dimension.

What ’ s the difference between absolute and comparative XPath?

+
  • Absolute XPathkickoff from the root node (e.g.,/html/body/div [2] /form/input). It breaks easily if the DOM construction displacement.

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

 

How can I capture XPath mechanically 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 property for stability.

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

 

 

Explain

|

Trinh Huynh
Proficient Writer, Katalon
Trinh Huynh is an experienced proficient writer in the software testing industry. She immingle her extensive technological knowledge with deep understanding of the Katalon products to make guides that help testers of all point.

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free