{"id":361,"date":"2022-07-11T06:51:15","date_gmt":"2022-07-11T06:51:15","guid":{"rendered":"https:\/\/www.testleaf.com\/blog\/?p=361"},"modified":"2026-03-06T14:03:29","modified_gmt":"2026-03-06T08:33:29","slug":"beginners-guide-xpath-in-selenium","status":"publish","type":"post","link":"https:\/\/www.testleaf.com\/blog\/beginners-guide-xpath-in-selenium\/","title":{"rendered":"Complete Beginners Guide for XPath In Selenium Automation"},"content":{"rendered":"<div style=\"margin-top: 0px; margin-bottom: 0px;\" class=\"sharethis-inline-share-buttons\" ><\/div><!--[if lt IE 9]><script>document.createElement('audio');<\/script><![endif]-->\n<audio class=\"wp-audio-shortcode\" id=\"audio-361-1\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/mpeg\" src=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Complete-Beginners-Guide-for-XPath-In-Selenium-Automation.mp3?_=1\" \/><a href=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Complete-Beginners-Guide-for-XPath-In-Selenium-Automation.mp3\">https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Complete-Beginners-Guide-for-XPath-In-Selenium-Automation.mp3<\/a><\/audio>\n<h2><\/h2>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.testleaf.com\/blog\/beginners-guide-xpath-in-selenium\/#Introduction\" >Introduction\u00a0<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.testleaf.com\/blog\/beginners-guide-xpath-in-selenium\/#Key_Takeaways\" >Key Takeaways<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.testleaf.com\/blog\/beginners-guide-xpath-in-selenium\/#What_is_XPath_in_Selenium\" >What is XPath in Selenium?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.testleaf.com\/blog\/beginners-guide-xpath-in-selenium\/#Why_do_we_go_for_XPath\" >Why do we go for XPath?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.testleaf.com\/blog\/beginners-guide-xpath-in-selenium\/#How_do_we_write_XPath\" >How do we write XPath?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.testleaf.com\/blog\/beginners-guide-xpath-in-selenium\/#Types_of_XPATH\" >Types of XPATH<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.testleaf.com\/blog\/beginners-guide-xpath-in-selenium\/#FAQs\" >FAQs<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"mb-0\"><span class=\"ez-toc-section\" id=\"Introduction\"><\/span><strong>Introduction\u00a0<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li><strong>XPath in Selenium<\/strong> : X-path refers to the XML path. XML path provides the precise location of the Web Element. It helps to traverse the HTML structure from the origin to the destination of the webpage to find the exact position of the Web Element.<\/li>\n<li>The XML (<strong><b>eXtensible Markup Language<\/b><\/strong>) Path Language (XPath) is used\u00a0<strong><b>to identify or address parts of an XML document uniquely<\/b><\/strong>.<\/li>\n<li>An XPath expression can be used to search through an XML document and extract information from any part of the document, such as an element or attribute in it.<\/li>\n<li>In this guide we will walk you through to learn how to use XPath in selenium<\/li>\n<\/ul>\n<p data-start=\"1911\" data-end=\"2295\">XPath is a locator strategy in Selenium that finds elements by navigating the HTML DOM using attributes, text, and relationships. Use it when IDs\/classes aren\u2019t stable. Prefer short, relative XPath anchored to stable attributes or visible text, and avoid long absolute paths and raw indexing unless the element is tied to a reliable parent.<\/p>\n<h2 data-start=\"2297\" data-end=\"2329\"><span class=\"ez-toc-section\" id=\"Key_Takeaways\"><\/span><strong>Key Takeaways<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p data-start=\"2330\" data-end=\"2369\">Place immediately after the Answer Box:<\/p>\n<ul data-start=\"2370\" data-end=\"2738\">\n<li data-start=\"2370\" data-end=\"2488\">\n<p data-start=\"2372\" data-end=\"2488\">Prefer <strong data-start=\"2379\" data-end=\"2397\">relative XPath<\/strong> with stable attributes\/text; avoid absolute paths.<\/p>\n<\/li>\n<li data-start=\"2489\" data-end=\"2614\">\n<p data-start=\"2491\" data-end=\"2614\">Use <strong data-start=\"2495\" data-end=\"2541\">contains()\/starts-with()\/normalize-space()<\/strong> for dynamic UI text and spacing.<\/p>\n<\/li>\n<li data-start=\"2615\" data-end=\"2738\">\n<p data-start=\"2617\" data-end=\"2738\">Avoid raw indexing unless anchored to a unique parent (table row\/card\/container).<\/p>\n<\/li>\n<\/ul>\n<p>XPath failures happen more in CI\/cloud runs because UI timing varies by region (latency), device\/viewport defaults, and geo-based banners (cookie\/locale offers). For India\/global traffic, always wait for \u201cready-to-click\u201d state and close overlays before using XPath clicks.<\/p>\n<h2 class=\"mb-0\"><span class=\"ez-toc-section\" id=\"What_is_XPath_in_Selenium\"><\/span><strong>What is XPath in Selenium?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li><a href=\"https:\/\/www.testleaf.com\/blog\/6-ways-to-use-xpath-in-selenium-webdriver\/\">XPath in selenium<\/a> is an XML path used for navigation through the HTML structure of the page.<\/li>\n<li>Selenium uses different ways to locate the web elements. The primary way to locate the element is by using the basic locator&#8217;s id, name, class name, link text, and tag name.<\/li>\n<li>Sometimes the web elements in the Dom will not have proper attribute values. For example, the attribute id will have auto-generated numbers; likewise, names, tag names, and class names will sometimes be duplicated, and the values will have special characters and white space. At that time, I will go with XPath.<\/li>\n<\/ul>\n<h2><a href=\"https:\/\/www.testleaf.com\/course\/selenium-automation-certification-training-course.html?utm_source=blog_post&amp;utm_medium=Organic&amp;utm_campaign=Blog_Post\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-5159 size-full\" src=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/06\/Selenium.jpg\" alt=\"Selenium training in chennai\" width=\"2048\" height=\"512\" srcset=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/06\/Selenium.jpg 2048w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/06\/Selenium-300x75.jpg 300w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/06\/Selenium-1024x256.jpg 1024w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/06\/Selenium-768x192.jpg 768w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/06\/Selenium-1536x384.jpg 1536w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/06\/Selenium-150x38.jpg 150w\" sizes=\"(max-width: 2048px) 100vw, 2048px\" \/><\/a><\/h2>\n<h2><\/h2>\n<h2><span class=\"ez-toc-section\" id=\"Why_do_we_go_for_XPath\"><\/span><strong>Why do we go for XPath?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><a href=\"https:\/\/www.testleaf.com\/course\/selenium-automation-certification-training-course.html\">Selenium automation<\/a> has primary locators like ID, name, class, link text, and partial link text. When the elements cannot be located, XPath is used to locate those elements on the webpage.<br \/>\nXpath is preferably considered when,<\/p>\n<ul>\n<li>There are no other standard locators available like id, name, etc.<\/li>\n<li>The locators are not stable.<\/li>\n<li>Having multiple values (duplicated).<\/li>\n<li><b><\/b><strong><b>Id= &#8220;ext-gen869&#8221;-<\/b><\/strong>even though the id is considered a reliable locator, we cannot use it as it contains a number and is dynamic (subject to change).<\/li>\n<li><b><\/b><strong><b>Class=&#8221;x-grid3-col x-grid3-cell x-grid3-to-friendlyPartyName\u201d<\/b><\/strong>-&gt; the class name has special character it also cannot be used as basic locator to identify this particular element. In these cases, XPath methods accurately locate the web elements.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"How_do_we_write_XPath\"><\/span><strong>How do we write XPath?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<table>\n<tbody>\n<tr>\n<td width=\"165\"><strong><b>Expression<\/b><\/strong><\/td>\n<td width=\"337\"><strong><b>Description<\/b><\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"165\"><em><i>node name<\/i><\/em><\/td>\n<td width=\"337\">Selects all nodes with the name &#8220;<em><i>node name<\/i><\/em>&#8220;<\/td>\n<\/tr>\n<tr>\n<td width=\"165\">\/<\/td>\n<td width=\"337\">Selects from the root node<\/td>\n<\/tr>\n<tr>\n<td width=\"165\">\/\/<\/td>\n<td width=\"337\">Selects nodes in the document from the current node.<\/td>\n<\/tr>\n<tr>\n<td width=\"165\">@<\/td>\n<td width=\"337\">Selects attributes<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span class=\"ez-toc-section\" id=\"Types_of_XPATH\"><\/span><strong>Types of XPATH<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Based on the logic, XPath is broadly classified into two categories<\/p>\n<ol>\n<li>Absolute XPath<\/li>\n<li>Relative XPath<\/li>\n<\/ol>\n<h3><strong>1.Absolute XPath<\/strong><\/h3>\n<p><strong><b>Absolute XPath<\/b><\/strong>\u00a0-&gt;locates the element starting from the root element (HTML) to the destined web Element.<\/p>\n<ul>\n<li><b><\/b><strong><b>Absolute XPath<\/b><\/strong>-&gt; refers to the direct way of finding an element. The major drawback of Absolute XPath is that the XPath will fail if there are any changes in the element&#8217;s path.<\/li>\n<li>The XPath begins with a single forward-slash (\/), which states that the element can be selected from the root node. The root node is &lt;html&gt; following. We can write node by node to get the exact match<\/li>\n<\/ul>\n<p><strong>How to write Absolute XPath:<\/strong><br \/>\n(\/html\/body\/div\/div\/div\/div[2]\/div\/div[2]\/div\/div[1]\/div\/form\/div\/div\/div\/div\/div\/div\/input)[1]<\/p>\n<figure id=\"attachment_362\" aria-describedby=\"caption-attachment-362\" style=\"width: 1244px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-362 size-full\" src=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/How-to-write-Absolute-XPath.png\" alt=\"How to Use XPath in Selenium\" width=\"1244\" height=\"542\" srcset=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/How-to-write-Absolute-XPath.png 1244w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/How-to-write-Absolute-XPath-300x131.png 300w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/How-to-write-Absolute-XPath-1024x446.png 1024w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/How-to-write-Absolute-XPath-768x335.png 768w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/How-to-write-Absolute-XPath-150x65.png 150w\" sizes=\"(max-width: 1244px) 100vw, 1244px\" \/><figcaption id=\"caption-attachment-362\" class=\"wp-caption-text\">How to Use XPath in Selenium<\/figcaption><\/figure>\n<h3 class=\"mb-0\"><strong><b>2.Relative XPath<\/b><\/strong><\/h3>\n<ul>\n<li>Relative XPath &#8211; starts from the middle of the HTML DOM structure. It begins with a double forward-slash (\/\/).<\/li>\n<li>It can search elements anywhere on the webpage, which means there is no need to write a long path, and you can start from the middle of the HTML DOM structure.<\/li>\n<li>Relative XPath is always preferred as it is not a complete path from the root element.<\/li>\n<\/ul>\n<p class=\"mb-0\"><strong>Relative XPath We can write by using the following ways,<\/strong><\/p>\n<ol>\n<li>Attribute Based XPath<\/li>\n<li>Text() Based XPath<\/li>\n<li>Contains() Based XPath<\/li>\n<li>Collection Based XPath<\/li>\n<\/ol>\n<h4><strong>1. Attribute-Based XPath<\/strong><\/h4>\n<p>We locate a particular node in the DOM page by using the attribute.<\/p>\n<p><strong>Syntax to write the Attribute Based XPath:<\/strong><\/p>\n<figure id=\"attachment_363\" aria-describedby=\"caption-attachment-363\" style=\"width: 1038px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-363 size-full\" src=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Syntax-to-write.png\" alt=\"How to Use XPath in Selenium\" width=\"1038\" height=\"369\" srcset=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Syntax-to-write.png 1038w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Syntax-to-write-300x107.png 300w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Syntax-to-write-1024x364.png 1024w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Syntax-to-write-768x273.png 768w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Syntax-to-write-150x53.png 150w\" sizes=\"(max-width: 1038px) 100vw, 1038px\" \/><figcaption id=\"caption-attachment-363\" class=\"wp-caption-text\">XPath in Selenium<\/figcaption><\/figure>\n<p><strong>Example:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-364 size-full\" src=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Example.png\" alt=\"How to Use XPath in Selenium\" width=\"1240\" height=\"606\" srcset=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Example.png 1240w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Example-300x147.png 300w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Example-1024x500.png 1024w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Example-768x375.png 768w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Example-150x73.png 150w\" sizes=\"(max-width: 1240px) 100vw, 1240px\" \/><\/p>\n<p><strong>How to apply XPath as a Script<\/strong><br \/>\ndriver.findElement(By.xpath(&#8220;\/\/input [@id=\u2019email\u2019]&#8221;)<u>);<\/u><\/p>\n<h4><strong>2. Text () Based XPath<\/strong><\/h4>\n<p>The XPath Text () method is used to locate the element on a web page using the web element&#8217;s text. The function proves its worth if the element contains a text.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-365 size-full\" src=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/10\/Text-Based-Xpath.png\" alt=\"How to Use XPath in Selenium\" width=\"1277\" height=\"549\" \/><\/p>\n<p><strong>Syntax for text () method:<\/strong><\/p>\n<p><strong>\/\/tag name [text () =\u2019black colour text\u2019]<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-366 size-full\" src=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/10\/Syntax-for-text-method.png\" alt=\"How to Use XPath in Selenium\" width=\"1222\" height=\"716\" \/><\/p>\n<h4><strong>3. Contains () Based XPath<\/strong><\/h4>\n<p>When the element attributes are changing dynamically. Using contains, we can select elements based on their partial texts. It uses\u00a0<strong><b>Contains ()<\/b><\/strong>\u00a0method to match the partial value of the attribute. Partial match XPath is of two types<br \/>\n<strong><b>a) Partial Text Based XPath: <\/b><\/strong><strong><b>=&gt;uses \u00a0the partial text value of the web Element<\/b><\/strong><\/p>\n<p><strong>Syntax <\/strong><strong><b>Partial Text Based XPath<\/b><\/strong><strong>:<\/strong><br \/>\n<strong><b>\/\/tag name [contains (text (),&#8217; Partial text in my DOM&#8217; &#8216;)]<\/b><\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-367 size-full\" src=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Based-Xpath.png\" alt=\"How to Use XPath in Selenium\" width=\"1117\" height=\"599\" srcset=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Based-Xpath.png 1117w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Based-Xpath-300x161.png 300w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Based-Xpath-1024x549.png 1024w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Based-Xpath-768x412.png 768w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Based-Xpath-150x80.png 150w\" sizes=\"(max-width: 1117px) 100vw, 1117px\" \/><\/p>\n<p><strong><b>Example:<\/b><\/strong><\/p>\n<p><strong><b>\/\/ label [contains (text (),&#8217;Create &#8216;)]<\/b><\/strong><\/p>\n<p><strong><b>Partial attribute Based XPath: <\/b><\/strong><strong><b>=&gt;uses the partial attribute value of the Web Element<\/b><\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-369 size-full\" src=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Partial-attribute.png\" alt=\"How to Use XPath in Selenium\" width=\"1065\" height=\"591\" srcset=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Partial-attribute.png 1065w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Partial-attribute-300x166.png 300w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Partial-attribute-1024x568.png 1024w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Partial-attribute-768x426.png 768w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Partial-attribute-150x83.png 150w\" sizes=\"(max-width: 1065px) 100vw, 1065px\" \/><\/p>\n<p><strong><b>\/\/tag name [contains (@attribute, &#8216;Partial value of the attribute&#8217;)]<\/b><\/strong><\/p>\n<p><strong><b>Example<\/b><\/strong><br \/>\n<strong><b>\/\/input [contains (@id, \u2018password&#8217;)]<\/b><\/strong><\/p>\n<h4><strong>4. Collection Based XPath<\/strong><\/h4>\n<p>The collection-based path is usually used when there are multiple matches of a Web Element in the Dom. It uses index values to locate the particular match. Index of the XPath starts from 1.<\/p>\n<p><strong>Syntax Collection Based XPath<\/strong><br \/>\n<strong>(Any Valid XPath)[Index]<\/strong><\/p>\n<p>Example:<\/p>\n<p>(\/\/tag name [@attribute=\u2019value\u2019])[1]<\/p>\n<p>(Or)<\/p>\n<p>(\/\/tag name [text () =\u2019text here\u2019])[1]<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-370 size-full\" src=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Syntax-Collection-Based-Xpath-1.png\" alt=\"How to Use XPath in Selenium\" width=\"1121\" height=\"605\" srcset=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Syntax-Collection-Based-Xpath-1.png 1121w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Syntax-Collection-Based-Xpath-1-300x162.png 300w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Syntax-Collection-Based-Xpath-1-1024x553.png 1024w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Syntax-Collection-Based-Xpath-1-768x414.png 768w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Syntax-Collection-Based-Xpath-1-150x81.png 150w\" sizes=\"(max-width: 1121px) 100vw, 1121px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-371 size-full\" src=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Syntax-Collection-Based-Xpath-2.png\" alt=\"How to Use XPath in Selenium\" width=\"1137\" height=\"397\" srcset=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Syntax-Collection-Based-Xpath-2.png 1137w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Syntax-Collection-Based-Xpath-2-300x105.png 300w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Syntax-Collection-Based-Xpath-2-1024x358.png 1024w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Syntax-Collection-Based-Xpath-2-768x268.png 768w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2022\/07\/Syntax-Collection-Based-Xpath-2-150x52.png 150w\" sizes=\"(max-width: 1137px) 100vw, 1137px\" \/><\/p>\n<p><strong><b>*Note: if the Index is not mentioned, then the driver matches the first resulting match of the Web Element.<\/b><\/strong><\/p>\n<p><strong><b>Exceptions\u00a0<\/b><\/strong><br \/>\nThe exception you will get when the driver could not locate the right Web Element<br \/>\n*No Such Exception-&gt;not a valid XPath<\/p>\n<p>*Invalid Selector-&gt;refers to the syntax error in the XPath<\/p>\n<h3><strong><b>Wrapping Up<\/b><\/strong><\/h3>\n<p>If you are looking for a great way to learn how to use XPath and Selenium to automate your testing, check out our <a href=\"https:\/\/www.testleaf.com\/course\/selenium-automation-certification-training-course.html\"><u>Selenium WebDriver training course.<\/u><\/a>\u00a0This training course will teach you everything you need to know to use XPath and Selenium effectively.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"FAQs\"><\/span><strong>FAQs<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\t<div class=\"tlfaq\" id=\"tlfaq-0b808259-b9f2-445c-aa93-4eaae67942e4\"\n\t     data-single-open=\"1\">\n\t\t\n\t\t<div class=\"tlfaq__items\" role=\"region\" aria-label=\"FAQ\">\n\t\t\t\t\t\t\t<details class=\"tlfaq__item\" open id=\"tlfaq-0b808259-b9f2-445c-aa93-4eaae67942e4-0\">\n\t\t\t\t\t<summary class=\"tlfaq__question\">\n\t\t\t\t\t\t<span class=\"tlfaq__qtext\">What is XPath in Selenium and why is it used?<\/span>\n\t\t\t\t\t\t<span class=\"tlfaq__icon\" aria-hidden=\"true\"><\/span>\n\t\t\t\t\t<\/summary>\n\t\t\t\t\t<div class=\"tlfaq__answer\">\n\t\t\t\t\t\tXPath is a locator strategy in Selenium used to find elements by navigating the HTML DOM using attributes, text, and relationships. It is especially useful when IDs or classes are missing, dynamic, or not unique.\t\t\t\t\t<\/div>\n\t\t\t\t<\/details>\n\t\t\t\t\t\t\t\t<details class=\"tlfaq__item\"  id=\"tlfaq-0b808259-b9f2-445c-aa93-4eaae67942e4-1\">\n\t\t\t\t\t<summary class=\"tlfaq__question\">\n\t\t\t\t\t\t<span class=\"tlfaq__qtext\">Which is better: XPath or CSS Selector in Selenium?<\/span>\n\t\t\t\t\t\t<span class=\"tlfaq__icon\" aria-hidden=\"true\"><\/span>\n\t\t\t\t\t<\/summary>\n\t\t\t\t\t<div class=\"tlfaq__answer\">\n\t\t\t\t\t\tCSS selectors are usually faster and cleaner for most cases. XPath is better when you need to locate elements using visible text or navigate through parent, ancestor, or sibling relationships.\t\t\t\t\t<\/div>\n\t\t\t\t<\/details>\n\t\t\t\t\t\t\t\t<details class=\"tlfaq__item\"  id=\"tlfaq-0b808259-b9f2-445c-aa93-4eaae67942e4-2\">\n\t\t\t\t\t<summary class=\"tlfaq__question\">\n\t\t\t\t\t\t<span class=\"tlfaq__qtext\">What is the difference between absolute and relative XPath?<\/span>\n\t\t\t\t\t\t<span class=\"tlfaq__icon\" aria-hidden=\"true\"><\/span>\n\t\t\t\t\t<\/summary>\n\t\t\t\t\t<div class=\"tlfaq__answer\">\n\t\t\t\t\t\tAbsolute XPath starts from the root of the HTML document, such as \/html\/body, and can break easily when the UI changes. Relative XPath starts from a stable element, such as \/\/div[@id=&#8217;login&#8217;]\/\/button, and is more maintainable.\t\t\t\t\t<\/div>\n\t\t\t\t<\/details>\n\t\t\t\t\t\t\t\t<details class=\"tlfaq__item\"  id=\"tlfaq-0b808259-b9f2-445c-aa93-4eaae67942e4-3\">\n\t\t\t\t\t<summary class=\"tlfaq__question\">\n\t\t\t\t\t\t<span class=\"tlfaq__qtext\">How do you write dynamic XPath for changing IDs or classes?<\/span>\n\t\t\t\t\t\t<span class=\"tlfaq__icon\" aria-hidden=\"true\"><\/span>\n\t\t\t\t\t<\/summary>\n\t\t\t\t\t<div class=\"tlfaq__answer\">\n\t\t\t\t\t\tTo write dynamic XPath, avoid full changing values and use stable patterns such as contains(@id,&#8217;partial&#8217;), starts-with(@id,&#8217;prefix&#8217;), stable attributes like data-testid or aria-label, or visible text using text() and contains(text(),&#8217;Login&#8217;).\t\t\t\t\t<\/div>\n\t\t\t\t<\/details>\n\t\t\t\t\t\t\t\t<details class=\"tlfaq__item\"  id=\"tlfaq-0b808259-b9f2-445c-aa93-4eaae67942e4-4\">\n\t\t\t\t\t<summary class=\"tlfaq__question\">\n\t\t\t\t\t\t<span class=\"tlfaq__qtext\">How do you use contains(), starts-with(), and text() in XPath?<\/span>\n\t\t\t\t\t\t<span class=\"tlfaq__icon\" aria-hidden=\"true\"><\/span>\n\t\t\t\t\t<\/summary>\n\t\t\t\t\t<div class=\"tlfaq__answer\">\n\t\t\t\t\t\tcontains() matches partial attribute values, starts-with() matches prefix-based values, and text() matches visible text exactly. For example, contains(@class,&#8217;btn&#8217;), starts-with(@id,&#8217;user_&#8217;), and \/\/button[text()=&#8217;Login&#8217;]. You can also use normalize-space() when text has extra spaces.\t\t\t\t\t<\/div>\n\t\t\t\t<\/details>\n\t\t\t\t\t\t\t\t<details class=\"tlfaq__item\"  id=\"tlfaq-0b808259-b9f2-445c-aa93-4eaae67942e4-5\">\n\t\t\t\t\t<summary class=\"tlfaq__question\">\n\t\t\t\t\t\t<span class=\"tlfaq__qtext\">Why does XPath work locally but fail in CI?<\/span>\n\t\t\t\t\t\t<span class=\"tlfaq__icon\" aria-hidden=\"true\"><\/span>\n\t\t\t\t\t<\/summary>\n\t\t\t\t\t<div class=\"tlfaq__answer\">\n\t\t\t\t\t\tXPath may work locally but fail in CI because CI environments are often slower and UI timing can differ due to network delays, animations, or overlays. In such cases, use explicit waits and handle popups or banners properly.\t\t\t\t\t<\/div>\n\t\t\t\t<\/details>\n\t\t\t\t\t\t\t\t<details class=\"tlfaq__item\"  id=\"tlfaq-0b808259-b9f2-445c-aa93-4eaae67942e4-6\">\n\t\t\t\t\t<summary class=\"tlfaq__question\">\n\t\t\t\t\t\t<span class=\"tlfaq__qtext\">What is the best XPath for tables and lists?<\/span>\n\t\t\t\t\t\t<span class=\"tlfaq__icon\" aria-hidden=\"true\"><\/span>\n\t\t\t\t\t<\/summary>\n\t\t\t\t\t<div class=\"tlfaq__answer\">\n\t\t\t\t\t\tThe best XPath for tables and lists is to anchor to a unique row or card text and then move to the target element. For example, locate a table row by cell text or a list card by its title, then select the related button or link inside it.\t\t\t\t\t<\/div>\n\t\t\t\t<\/details>\n\t\t\t\t\t\t<\/div>\n\n\t\t\t\t\t<script type=\"application\/ld+json\">\n\t\t\t\t{\"@context\":\"https:\/\/schema.org\",\"@type\":\"FAQPage\",\"mainEntity\":[{\"@type\":\"Question\",\"name\":\"What is XPath in Selenium and why is it used?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"XPath is a locator strategy in Selenium used to find elements by navigating the HTML DOM using attributes, text, and relationships. It is especially useful when IDs or classes are missing, dynamic, or not unique.\"}},{\"@type\":\"Question\",\"name\":\"Which is better: XPath or CSS Selector in Selenium?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"CSS selectors are usually faster and cleaner for most cases. XPath is better when you need to locate elements using visible text or navigate through parent, ancestor, or sibling relationships.\"}},{\"@type\":\"Question\",\"name\":\"What is the difference between absolute and relative XPath?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Absolute XPath starts from the root of the HTML document, such as \/html\/body, and can break easily when the UI changes. Relative XPath starts from a stable element, such as \/\/div[@id=&#8217;login&#8217;]\/\/button, and is more maintainable.\"}},{\"@type\":\"Question\",\"name\":\"How do you write dynamic XPath for changing IDs or classes?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"To write dynamic XPath, avoid full changing values and use stable patterns such as contains(@id,&#8217;partial&#8217;), starts-with(@id,&#8217;prefix&#8217;), stable attributes like data-testid or aria-label, or visible text using text() and contains(text(),&#8217;Login&#8217;).\"}},{\"@type\":\"Question\",\"name\":\"How do you use contains(), starts-with(), and text() in XPath?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"contains() matches partial attribute values, starts-with() matches prefix-based values, and text() matches visible text exactly. For example, contains(@class,&#8217;btn&#8217;), starts-with(@id,&#8217;user_&#8217;), and \/\/button[text()=&#8217;Login&#8217;]. You can also use normalize-space() when text has extra spaces.\"}},{\"@type\":\"Question\",\"name\":\"Why does XPath work locally but fail in CI?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"XPath may work locally but fail in CI because CI environments are often slower and UI timing can differ due to network delays, animations, or overlays. In such cases, use explicit waits and handle popups or banners properly.\"}},{\"@type\":\"Question\",\"name\":\"What is the best XPath for tables and lists?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"The best XPath for tables and lists is to anchor to a unique row or card text and then move to the target element. For example, locate a table row by cell text or a list card by its title, then select the related button or link inside it.\"}}]}\t\t\t<\/script>\n\t\t\t<\/div>\n\t\n<p data-start=\"2163\" data-end=\"2261\"><code data-start=\"2187\" data-end=\"2261\"><\/code><\/p>\n<h5><strong>We Also Provide Training In:<\/strong><\/h5>\n<ul>\n<li><a href=\"https:\/\/www.testleaf.com\/course\/selenium-automation-certification-training-course.html\"><strong>Advanced Selenium Training<\/strong><\/a><\/li>\n<li><a href=\"https:\/\/www.testleaf.com\/course\/playwright.html\"><strong>Playwright Training<\/strong><\/a><\/li>\n<li><a href=\"https:\/\/www.testleaf.com\/course\/genai-qa-engineers-training-course.html\"><strong>Gen AI Training<\/strong><\/a><\/li>\n<li><a href=\"https:\/\/www.testleaf.com\/course\/aws-cloud-architect-certification-training-course.html\"><strong>AWS Training<\/strong><\/a><\/li>\n<li><a href=\"https:\/\/www.testleaf.com\/course\/rest-api-testing-certification-training-course.html\"><strong>REST API Training<\/strong><\/a><\/li>\n<li><a href=\"https:\/\/www.testleaf.com\/course\/full-stack-developer-certification-training-course.html\"><strong>Full Stack Training<\/strong><\/a><\/li>\n<li><a href=\"https:\/\/www.testleaf.com\/course\/appium-mobile-automation-certification-training-course.html\"><strong>Appium Training<\/strong><\/a><\/li>\n<li><a href=\"https:\/\/www.testleaf.com\/course\/dev-ops-master-certification-training-course.html\"><strong>DevOps Training<\/strong><\/a><\/li>\n<li><a href=\"https:\/\/www.testleaf.com\/course\/apache-jmeter-testing-training-course.html\"><strong>JMeter Performance Training<\/strong><\/a><\/li>\n<\/ul>\n<h6><strong>Author\u2019s Bio<\/strong>:<\/h6>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2404 alignleft\" src=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2023\/04\/Untitled-design.png\" sizes=\"(max-width: 250px) 100vw, 250px\" srcset=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2023\/04\/Untitled-design.png 250w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2023\/04\/Untitled-design-150x150.png 150w, https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2023\/04\/Untitled-design-96x96.png 96w\" alt=\"\" width=\"250\" height=\"250\" \/><\/p>\n<p>As CEO of TestLeaf, I\u2019m dedicated to transforming software testing by empowering individuals with real-world skills and advanced technology. With 24+ years in software engineering, I lead our mission to shape local talent into global software professionals. Join us in redefining the future of test engineering and making a lasting impact in the tech world.<\/p>\n<p><strong>Babu Manickam<\/strong><\/p>\n<p>CEO \u2013 Testleaf<\/p>\n<p><a href=\"https:\/\/in.linkedin.com\/in\/babu-manickam\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.testleaf.com\/blog\/wp-content\/uploads\/2025\/07\/linkedin.png\" alt=\"LinkedIn Logo\" width=\"28\" height=\"28\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction\u00a0 XPath in Selenium : X-path refers to the XML path. XML path provides the precise location of the Web Element. It helps to traverse the HTML structure from the origin to the destination of the webpage to find the exact position of the Web Element. The XML (eXtensible Markup Language) Path Language (XPath) is &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/www.testleaf.com\/blog\/beginners-guide-xpath-in-selenium\/\"> <span class=\"screen-reader-text\">Complete Beginners Guide for XPath In Selenium Automation<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":372,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-sidebar-layout":"right-sidebar","site-content-layout":"plain-container","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"categories":[16],"tags":[786,70,29,102],"class_list":["post-361","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium","tag-java-selenium","tag-learn-selenium","tag-selenium","tag-software-testing-course"],"acf":[],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.testleaf.com\/blog\/wp-json\/wp\/v2\/posts\/361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testleaf.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testleaf.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testleaf.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testleaf.com\/blog\/wp-json\/wp\/v2\/comments?post=361"}],"version-history":[{"count":21,"href":"https:\/\/www.testleaf.com\/blog\/wp-json\/wp\/v2\/posts\/361\/revisions"}],"predecessor-version":[{"id":9471,"href":"https:\/\/www.testleaf.com\/blog\/wp-json\/wp\/v2\/posts\/361\/revisions\/9471"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.testleaf.com\/blog\/wp-json\/wp\/v2\/media\/372"}],"wp:attachment":[{"href":"https:\/\/www.testleaf.com\/blog\/wp-json\/wp\/v2\/media?parent=361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testleaf.com\/blog\/wp-json\/wp\/v2\/categories?post=361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testleaf.com\/blog\/wp-json\/wp\/v2\/tags?post=361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}