Getting Started with XPath in Selenium

Sauce AI for Test Authoring: Move from design to executing in minutes.|xBack to ResourcesBlogPosted

April 28, 2026 · 8 min read · Tool Comparison

Sauce AI for Test Authoring: Move from design to executing in minutes.

|

x

Back to Resources

Blog

Posted June 1, 2023

Getting Started with XPath in Selenium

Selenium is the industry-standard, open-source essay automation framework. This blog explains how to use XPath as a web element locator in Selenium.

Documentation

What is Selenium?

is a popular open-source fabric developed specifically for of web applications. supports, including Node.js, Java, Python, Ruby, and C #, countenance developers, QA, and still project handler to develop and review examination for their apps, race up the time to market.

Selenium 4, the latest version of the Selenium test tool, natively allows developer and quizzer to publish test script in different programming words (Python, Java, Ruby, C #, NodeJS, etc.) that can without modification.

For more info on Selenium 4, see the next resources:

What is XPath in Selenium?

XPath is a query lyric that can be employ to. XPath is the preferred locator when other CSS locator (ID, Class, etc.) that identify elements or unique attribute are not launch in an XML/HTML document. XPath in Selenium postdate an XML path to navigate through the HTML construction of a web page.

Whenever a web page loads in a browser, it creates a Document Object Model (DOM) structure. An XPath look, or command, is.

The grow number of browser types and variant makes a feasible alternative to. The ideal test tool for developers who want to move to automated testing isSelenium WebDriver, which is a free collection of open-source coating programming interfaces (APIs) used to.

The WebDriver codification library furnish methods to notice dynamic web elements using a locator like XPath or others like ID, Class, or other CSS (Cascading Style Sheets) chooser. Although the XPath code is easy to obtain, it can be clumsy to indite, brittle, and awkward to reverse engineer. This has led to the wideruse of CSS selectors to identify targetin WebDriver. Despite feature an initial encyclopaedism bender, CSS selectors provide bindings that are easier to read, less unannealed, and more close integrated into the browser platform than XPath.

XPath in Selenium syntax instance

The following is an example of the general syntax of XPath in Selenium:

1
//Tagname [@ AttibuteName = ‘ value ’]

XPath in Selenium Python

As one of the oldest programming languages, has a long-established ball-shaped developer community that has liberate manypackages for testing automation, include pytest, unittest, doctest, and others. With the growing number of browsers, automatise cross-browser testing becomes necessary, specially for any changes that demand modification. Installing Selenium in Python ’ s high-level programming speech environment, together with Selenium WebDriver and other automation instrument, provide the functionality needed to run cross-browser tests.

XPath in Selenium Python syntax instance

The following is an example of the syntax for discover element in Selenium Python:

1
driver.find_element_by_xpath (& quot; //input [textbook () = & # x27; Selenium & # x27;] & quot;)

WebDriver for Pythonis a driver proxy for running Selenium testing mechanization in multiple framework, code editors, and IDEs. Python ’ s syntax in Selenium is both object-oriented and functional, allowing developer to use classes or functions. Selenium Python works with command-line creature, enabling developers to build continuous integration/continuous delivery (CI/CD) pipelines.

Finding Elements by XPath in Selenium

Before editing an HTML ingredient in the DOM structure, the emplacement of the specific web element first needs to be identified by its XML path. For this task,XPathis typically the easiest way to get started with locators in Selenium. XPath is also the preferred locater for inspecting elements on a page. If elements neglect to be identified by general locators like ID, name, class name, etc., then XPath can be habituate to extract info from those web elements in an XML or HTML document.

Other locators in Selenium can be used to search for elements utilise tatter or CSS category names. Although ID and CSS selectors may be the fast among the locators — it is potential to move from XPath to CSS — XPath in Selenium can handle more complex, dynamical searches.

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

Finding an factor in XPath syntax instance

The chase is an exemplar of the syntax for finding an element in XPath:

1
//tag_name [@ Attribute_name = “ Value of attribute ”]

Types of XPath in Selenium

There are two means to locate an element in XPath: Absolute XPath and Relative XPath. The paths are delineate by their XPath node, either the point where the path initiates to the node where the element is located or a point somewhere along the DOM.

Absolute path

XPath can be used to locate an element in absolute term. An absolute XPath expression contains the location of all ingredient from the root node (HTML), where the path starts, to the desired element. One challenge with this way eccentric is that the whole XPath will neglect to find the element if there is any alteration or adjustment of any node or tag along the outlined XPath expression.

Absolute XPath syntax representative

The following is an example of absolute XPath syntax, which forever begin with a single forward stroke, “ /. ”

1
/html/body/div [1] /header/div/div [1] /div [3] /div/form/div [3] /div [1] /input

Relative path

XPath can also use terms relative to an element that does have an ID or gens dimension. A relative XPath expression usually starts from the middle of the HTML DOM structure – there ’ s no need to commence from the beginning node. Proportional XPath aspect appear to be more reliable with less chance of a script break.

Relative XPath syntax representative

The following is an example of proportional XPath syntax, which always begins with a double forward slash, “ //. ”

1
// * [@ id= ” twotabsearchtextbox ”]

What are XPath Axes?

XPath axes are employ to notice dynamic ingredient when a normal XPath element search method like ID, name, class name, etc., is not potential. The axes are defined from the current context node in an XML document to the node where the XPath is indicate. The path refers to the axis on which elements are lie relative to another given element. The designation of elements is determined by their relationship like parent, baby, sibling, ancestor, and descendent to the context node.

How to Write a Dynamic XPath in Selenium

To write a dynamic XPath in Selenium, log into the site where the test will be done and find the XPath extension in the Chrome browser (without any plugin). If the element to be tested can not be found by general locators like ID, name, tag gens, class, or former attribute, use the XPath locater.

XPath in Selenium examples

1
1. Example syntax using a single attribute (relative XPath type):
2
3
// a [@ href= ’ http: //www.google.com ’]
4
//input [@ id= ’ name ’]
5
//input [@ name= ’ username ’]
6
//img [@ alt= ’ sometext ’]
7
8
2. Example syntax using multiple dimension (proportional XPath type):
9
10
//tagname [@ attribute1= ’ value1 ’] [attribute2= ’ value2 ’]
11
//a [@ id= ’ id1 ’] [@ name= ’ namevalue1 ’]
12
//img [@ src= ’ ’] [@ href= ’ ’]
13
14
3. Example syntax of a search using “ contains ” (here, to create an account):
15
16
//tagname [contains (@ attribute, ’ value1 ’)]
17
//input [contains (@ id, ’ ’)]
18
//input [contains (@ gens, ’ ’)]
19
//a [contains (@ href, ’ ’)]
20
//img [contains (@ src, ’ ’)]
21
//div [contains (@ id, ’ ’)]
22
23
4. Example syntax of a hunting using “ starts-with: ”:
24
25
//tagname [starts-with (@ attribute-name, ’ ’)]
26
//id [starts-with (@ id, ’ ’)]
27
//a [starts-with (@ href= ’ ’)]
28
//img [starts-with (@ src= ’ ’)]
29
//div [starts-with (@ id= ’ ’)]
30
//input [starts-with (@ id= ’ ’)]
31
//button [starts-with (@ id, ’ ’)]
32
33
5. Example syntax for expend the next node:
34
35
Xpath/following: :again-ur-regular-path
36
//input [@ id= ’ ’] /following: :input [1]
37
//a [@ href= ’ ’] /following: :a [1]
38
//img [@ src= ’ ’] /following: :img [1]
39
40
6. Example syntax for using the preceding node:
41
42
Xpath/preceding: :again-ur-regular-path
43
//input [@ id= ’ ’] / preceding: :input [1]
44
//a [@ href= ’ ’] / preceding: :a [1]
45
//img [@ src= ’ ’] / preceding: :img [1]
46
47
7. Example syntax for using the absolute XPath type:
48
49
1-/html/head/body/div/input
50
51
8. Example syntax for text search in XPath:
52
53
Syntax- tagname [textbook () = ’ schoolbook which is visible on page ’]
54
55
9. Example syntax for text search with ‘ contains ’:
56
57
/ * [contains (text (), ’ Employee Distribution by Subunit ’)]
58
Published:
Jun 1, 2023
Jump to content

What is Selenium?

What is XPath in Selenium?

XPath in Selenium Python

Finding Elements by XPath in Selenium

Types of XPath in Selenium

What are XPath Axes?

How to Write a Dynamic XPath in Selenium

Share this post
Copy Share Link

Ready to commence screen with Selenium and Sauce Labs?

Let & # x27; s go!

LinkedIn
© 2026 Sauce Labs Inc., all rights reserved. SAUCE and SAUCE LABS are registered trademarks possess by Sauce Labs Inc. in the United States, EU, and may be file in former jurisdictions.
robot
quote

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