{"id":8200,"date":"2021-01-30T16:29:28","date_gmt":"2021-01-30T10:59:28","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=8200"},"modified":"2025-12-15T08:08:50","modified_gmt":"2025-12-15T13:08:50","slug":"xpath-contains-and-or-parent-start-with-axes-in-selenium-webdriver","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/xpath-contains-and-or-parent-start-with-axes-in-selenium-webdriver\/","title":{"rendered":"XPath Contains, AND OR, Parent, Start with, Axes in Selenium Webdriver"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>Why XPath Skills Separate Good Testers from Great Testers<\/strong><\/h2>\n\n\n\n<p>In Selenium, the <strong><code>and<\/code> operator in XPath<\/strong> is a logical operator that allows you to combine multiple conditions to locate elements more accurately. An element is matched only when <strong>all specified conditions evaluate to true<\/strong>, making your locators more precise and reliable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax<\/h3>\n\n\n\n<p>The general syntax for using the <code>and<\/code> operator in a relative XPath is:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/tag_name[@attribute1='value1' and @attribute2='value2']\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Common Use Cases<\/h2>\n\n\n\n<p><strong>1. Identifying unique elements<\/strong><br>When a single attribute (such as <code>class<\/code>) is shared by multiple elements, combining conditions helps narrow down the exact target.<\/p>\n\n\n\n<p><strong>2. Mixing attributes with functions<\/strong><br>You can pair fixed attributes with XPath functions like <code>contains()<\/code> or <code>text()<\/code> to handle dynamic or partially changing elements.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Examples<\/h3>\n\n\n\n<p><strong>Multiple Attributes<\/strong><br>Select an input field that has both a specific <code>name<\/code> and <code>type<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/input[@name='username' and @type='text']\n<\/pre>\n\n\n\n<p><strong>Attribute and Text Content<\/strong><br>Locate a button with a particular <code>id<\/code> and visible text:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/button[@id='loginBtn' and text()='Login']\n<\/pre>\n\n\n\n<p><strong>Using <code>contains()<\/code> for Dynamic Elements<\/strong><br>Match elements with partially dynamic values:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/div[contains(@class, 'user-profile') and contains(@id, 'active')]\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Alternative Approach: Multiple Predicates<\/h2>\n\n\n\n<p>Instead of explicitly using the <code>and<\/code> keyword, you can apply multiple predicates (square brackets). XPath treats them as a logical AND.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example:<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/input[@type='submit'][@value='Login']\n<\/pre>\n\n\n\n<p>This is functionally the same as:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/input[@type='submit' and @value='Login']\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practices<\/h3>\n\n\n\n<p><strong>Keep It Readable:<\/strong> Combining conditions improves accuracy, but avoid overly complex XPaths that are difficult to maintain.<\/p>\n\n\n\n<p><strong>Use Relative XPath:<\/strong> Start with <code>\/\/<\/code> rather than absolute paths (<code>\/<\/code>) to make your tests more robust against UI changes.<\/p>\n\n\n\n<p><strong>Verify Before Use:<\/strong> Test your XPath in browser DevTools (Ctrl + Shift + I) using <code>$x(\"your_xpath\")<\/code> to confirm it works correctly.<\/p>\n\n\n\n<p>Industry reports show that over 78% of QA teams still rely on Selenium WebDriver, and XPath remains the most used locator strategy across web automation frameworks. As UI patterns evolve, XPath mastery becomes even more important. That is why learning XPath Contains and other advanced functions becomes a key skill when you join a <a href=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\" data-type=\"link\" data-id=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\">Selenium certification course <\/a>or a Selenium course online.<\/p>\n\n\n\n<p>In this guide, you will learn everything about:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>XPath Contains<\/li>\n\n\n\n<li>xpath contains with AND\/OR<\/li>\n\n\n\n<li>Parent and ancestor navigation<\/li>\n\n\n\n<li>Starts-with usage<\/li>\n\n\n\n<li>XPath axes (following, preceding, sibling, descendant, etc.)<\/li>\n\n\n\n<li>Real project examples<\/li>\n\n\n\n<li>Step-by-step usage patterns<\/li>\n<\/ul>\n\n\n\n<p>By the end of this blog, you will write XPath expressions that work on any website, even with dynamic or unstable DOM structures.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What Is XPath and Why Testers Rely on It?<\/strong><\/h2>\n\n\n\n<p>XPath stands for <a href=\"https:\/\/en.wikipedia.org\/wiki\/XML\" data-type=\"link\" data-id=\"https:\/\/en.wikipedia.org\/wiki\/XML\" rel=\"nofollow noopener\" target=\"_blank\">XML<\/a> Path Language, and it lets Selenium identify page elements using paths, functions, and hierarchical relationships. XPath works even when:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"300\" height=\"168\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/01\/images-19.jpg\" alt=\"XPath\" class=\"wp-image-32912\" style=\"aspect-ratio:1.785790379414898;width:529px;height:auto\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/01\/images-19.jpg 300w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/01\/images-19-150x84.jpg 150w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Elements have no ID<\/li>\n\n\n\n<li>Elements have auto-generated IDs<\/li>\n\n\n\n<li>Elements look identical<\/li>\n\n\n\n<li>Elements load dynamically<\/li>\n\n\n\n<li>Elements belong to nested structures<\/li>\n<\/ul>\n\n\n\n<p>This makes XPath the most flexible locator type in Selenium WebDriver.<\/p>\n\n\n\n<p>XPath is especially useful in automation frameworks built with:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>TestNG<\/li>\n\n\n\n<li>JUnit<\/li>\n\n\n\n<li>BDD (Cucumber)<\/li>\n\n\n\n<li>PyTest + Selenium<\/li>\n\n\n\n<li>Robot Framework<\/li>\n<\/ul>\n\n\n\n<p>If you enroll in a Selenium certification course or Selenium course online, XPath mastery is always a major part of the curriculum.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Understanding XPath Contains (Focus Keyword)<\/strong><\/h2>\n\n\n\n<p>The phrase <strong>XPath Contains<\/strong> or <strong>xpath contains<\/strong> refers to using the <code>contains()<\/code> function in XPath to identify elements with partial attribute values.<\/p>\n\n\n\n<p>This is the most used XPath feature in test automation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Why XPath Contains is important<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Helps locate elements with dynamic values<\/li>\n\n\n\n<li>Useful when IDs are partially changing<\/li>\n\n\n\n<li>Helps locate long attribute values<\/li>\n\n\n\n<li>Works perfectly for class names used with multiple words<\/li>\n\n\n\n<li>Supports fuzzy matching<\/li>\n\n\n\n<li>Reduces script breakage<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Syntax<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/tagname[contains(@attribute, 'value')]\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 1: Button with dynamic ID<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/button[contains(@id, 'login')]\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 2: Element with long class value<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/div[contains(@class, 'menu')]\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 3: Partial text matching<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/span[contains(text(), 'Welcome')]\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Real Project Example<\/strong><\/h3>\n\n\n\n<p>Suppose your product team updates button IDs dynamically:<\/p>\n\n\n\n<p><code>btn-login-25489<\/code><br><code>btn-login-85473<\/code><\/p>\n\n\n\n<p>Using a normal XPath breaks.<br>Using XPath Contains ensures your script remains stable.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/button[contains(@id, 'btn-login')]\n<\/pre>\n\n\n\n<p>This approach is taught in every Selenium certification course because industry-grade systems use dynamic attributes frequently.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Using XPath Contains with AND &amp; OR<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/01\/maxresdefault-48-1024x576.jpg\" alt=\"Using XPath\" class=\"wp-image-32178\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/01\/maxresdefault-48-1024x576.jpg 1024w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/01\/maxresdefault-48-300x169.jpg 300w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/01\/maxresdefault-48-768x432.jpg 768w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/01\/maxresdefault-48-150x84.jpg 150w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/01\/maxresdefault-48.jpg 1280w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Advanced testers combine XPath Contains with logical operators.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>XPath Contains + AND<\/strong><\/h3>\n\n\n\n<p>Use AND to match multiple conditions at once.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Syntax<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/tag[contains(@attribute1,'value1') and contains(@attribute2,'value2')]\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/input[contains(@class, 'form') and contains(@type, 'text')]\n<\/pre>\n\n\n\n<p>This is useful when an element has many similar siblings.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>XPath Contains + OR<\/strong><\/h3>\n\n\n\n<p>Use OR to match either condition.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Syntax<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/tag[contains(@attribute,'value1') or contains(@attribute,'value2')]\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/button[contains(text(),'Submit') or contains(text(),'Continue')]\n<\/pre>\n\n\n\n<p>This makes tests flexible when UI teams change button text during A\/B testing.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Using Starts-with() in Selenium WebDriver<\/strong><\/h2>\n\n\n\n<p>When attributes begin with predictable text, use the <code>starts-with()<\/code> function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Syntax<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/tag[starts-with(@attribute,'value')]\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/input[starts-with(@name,'user')]\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Why Start-with is Useful<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>IDs sometimes start with fixed text but end with dynamic numbers<\/li>\n\n\n\n<li>Helps locate dynamic React.js or Angular components<\/li>\n\n\n\n<li>Works well when SPAs generate repeating elements<\/li>\n<\/ul>\n\n\n\n<p>Many enterprise-level websites use <a href=\"https:\/\/www.h2kinfosys.com\/blog\/what-is-javascript\/\" data-type=\"link\" data-id=\"https:\/\/www.h2kinfosys.com\/blog\/what-is-javascript\/\">JavaScript<\/a> frameworks that auto-generate IDs. This makes xpath contains and starts-with absolutely essential.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Navigating Parent and Ancestor Nodes in XPath<\/strong><\/h2>\n\n\n\n<p>Sometimes, the element you want to target has no clear attributes. In such cases, testers locate child elements and then move upward to parent.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>The Parent Axis<\/strong><\/h4>\n\n\n\n<p>Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/childElement\/parent::tagname\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example: Select parent div of an input<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/input[@id='email']\/parent::div\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Real Use Case<\/strong><\/h3>\n\n\n\n<p>Many UI components keep the actual clickable element within a parent wrapper.<br>In such cases, direct XPath fails. Parent navigation solves this problem.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>ancestor:: Axis<\/strong><\/h2>\n\n\n\n<p>Ancestor moves upward multiple levels.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/span[text()='Total']\/ancestor::table\n<\/pre>\n\n\n\n<p>This is useful when:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Table structures are complex<\/li>\n\n\n\n<li>Elements belong to nested DIVs<\/li>\n\n\n\n<li>You need to find a related parent container<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>XPath Axes in Selenium WebDriver<\/strong><\/h2>\n\n\n\n<p>XPath axes allow you to navigate the DOM horizontally or vertically.<br>This provides extreme control in finding elements based on relationships.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>following:: Axis<\/strong><\/h3>\n\n\n\n<p>Select elements that appear after the current node.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/label[text()='Email']\/following::input[1]\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>preceding:: Axis<\/strong><\/h3>\n\n\n\n<p>Select elements that appear before the current node.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/button[text()='Submit']\/preceding::label[1]\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>following-sibling:: Axis<\/strong><\/h3>\n\n\n\n<p>Select siblings on the same level after the current node.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/label[@id='firstName']\/following-sibling::input\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>preceding-sibling:: Axis<\/strong><\/h3>\n\n\n\n<p>Select siblings before the current node.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/span[@id='price']\/preceding-sibling::label\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>descendant:: Axis<\/strong><\/h3>\n\n\n\n<p>Select nested child elements.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/div[@id='menu']\/descendant::a\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.6 child:: Axis<\/strong><\/h3>\n\n\n\n<p>Direct children only.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ul[@id='list']\/child::li\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5.7 parent:: Axis<\/strong><\/h3>\n\n\n\n<p>Moves one level up.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/span[@class='error']\/parent::div\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Practical, Real-World Examples<\/strong><\/h2>\n\n\n\n<p>Modern UI frameworks like React, Angular, and Vue generate dynamic DOMs.<br>Here are real automation scenarios where XPath Contains is essential.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example: Dynamic dropdown values<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/li[contains(@class, 'dropdown-item')]\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6.2 Example: Auto-generated input fields<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/input[starts-with(@id,'user_')]\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example: Tables with inconsistent column order<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/td[contains(text(),'Price')]\/following-sibling::td<br><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example: Extracting labels and values<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/label[contains(text(),'Email')]\/following::input[1]\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Explaining Advanced XPath Strategies (Step-by-Step)<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1: Identify a stable anchor<\/strong><\/h3>\n\n\n\n<p>Find an element that rarely changes.<br>Example: label text like \u201cEmail\u201d.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 2: Decide the direction<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Downward \u2192 descendant<\/li>\n\n\n\n<li>Upward \u2192 parent or ancestor<\/li>\n\n\n\n<li>Same level \u2192 sibling<\/li>\n\n\n\n<li>Forward \u2192 following<\/li>\n\n\n\n<li>Backward \u2192 preceding<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 3: Apply XPath Contains for fuzziness<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/div[contains(@class,'login')]\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 4: Combine with AND for precision<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/input[contains(@class,'form') and @type='text']\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 5: Use OR when A\/B UI varies<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/button[contains(text(),'Login') or contains(text(),'Sign In')]\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 6: Always validate XPath using browser dev tools<\/strong><\/h3>\n\n\n\n<p>Use:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Chrome DevTools<\/li>\n\n\n\n<li>Firefox Inspector<\/li>\n\n\n\n<li>SelectorsHub<\/li>\n\n\n\n<li>ChroPath<\/li>\n<\/ul>\n\n\n\n<p>This helps ensure accuracy.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Why XPath Mastery Helps You Get Better Jobs<\/strong><\/h2>\n\n\n\n<p>Companies expect automation engineers to handle unstable UI.<br>Recruiters often ask XPath questions like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Write XPath using contains<\/li>\n\n\n\n<li>Navigate to parent element<\/li>\n\n\n\n<li>Use axes to locate elements<\/li>\n\n\n\n<li>Handle dynamic IDs using starts-with<\/li>\n<\/ul>\n\n\n\n<p>Learning this boosts your confidence and improves your interview performance.<\/p>\n\n\n\n<p>Most students who join a Selenium certification course or Selenium course online improve their XPath skills within the first few weeks.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Case Study: How a QA Team Improved Script Stability by 60%<\/strong><\/h2>\n\n\n\n<p>A retail e-commerce company faced automation failures due to dynamic DOM updates.<br>IDs changed every release.<br>Testers replaced 40% of their failing locators with:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>XPath Contains<\/li>\n\n\n\n<li>Starts-with<\/li>\n\n\n\n<li>following-sibling<\/li>\n\n\n\n<li>parent<\/li>\n<\/ul>\n\n\n\n<p>Result:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>60% fewer locator failures<\/strong><\/li>\n\n\n\n<li><strong>30% faster script maintenance<\/strong><\/li>\n\n\n\n<li><strong>Higher automation coverage<\/strong><\/li>\n<\/ul>\n\n\n\n<p>XPath skills deliver strong ROI in enterprise automation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Beginner Mistakes to Avoid When Writing XPath<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Do not use absolute XPath<\/h3>\n\n\n\n<p>Always avoid:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/html\/body\/div\/table\/tr\/td\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Avoid indexing unless necessary<\/h3>\n\n\n\n<p>Use:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(\/\/input[@type='text'])[1]\n<\/pre>\n\n\n\n<p>only when no unique attributes exist.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Avoid too many OR conditions<\/h3>\n\n\n\n<p>Use clean, readable expressions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Best Practices for XPath in Selenium WebDriver<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Prefer relative XPath<\/li>\n\n\n\n<li>Use XPath Contains for dynamic attributes<\/li>\n\n\n\n<li>Use starts-with for evolving IDs<\/li>\n\n\n\n<li>Avoid deeply nested chains<\/li>\n\n\n\n<li>Use axes only when needed<\/li>\n\n\n\n<li>Test XPath in browser first<\/li>\n\n\n\n<li>Keep locators readable<\/li>\n<\/ul>\n\n\n\n<p>These habits are taught in every <a href=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\" data-type=\"link\" data-id=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\">Selenium course online<\/a> at H2K Infosys.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Summary of All XPath Techniques Covered<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"599\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/01\/Basic-XPath-syntax-in-Selenium-1024x599.webp\" alt=\"XPath Techniques\" class=\"wp-image-32182\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/01\/Basic-XPath-syntax-in-Selenium-1024x599.webp 1024w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/01\/Basic-XPath-syntax-in-Selenium-300x176.webp 300w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/01\/Basic-XPath-syntax-in-Selenium-768x449.webp 768w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/01\/Basic-XPath-syntax-in-Selenium-150x88.webp 150w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/01\/Basic-XPath-syntax-in-Selenium.webp 1150w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>You learned:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>How to use XPath Contains<\/li>\n\n\n\n<li>xpath contains with AND\/OR<\/li>\n\n\n\n<li>Using Starts-with<\/li>\n\n\n\n<li>Parent and ancestor navigation<\/li>\n\n\n\n<li>All major XPath axes<\/li>\n\n\n\n<li>Real project examples<\/li>\n\n\n\n<li>Best practices<\/li>\n\n\n\n<li>How XPath improves automation stability<\/li>\n<\/ul>\n\n\n\n<p>XPath Contains remains the most used and most powerful locator strategy in Selenium automation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Key Takeaways<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>XPath Contains helps locate dynamic or complex elements.<\/li>\n\n\n\n<li>Axes help navigate DOM structures easily.<\/li>\n\n\n\n<li>Starts-with and parent are essential for modern UI testing.<\/li>\n\n\n\n<li>XPath mastery improves your automation success rate.<\/li>\n\n\n\n<li>Selenium certification course training strengthens locator skills.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Take your XPath skills to the next level with practical, real-world training that prepares you for the toughest automation challenges.<br>Join H2K Infosys today to master Selenium automation with expert-led sessions, hands-on projects, and the confidence to excel in your QA career.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Why XPath Skills Separate Good Testers from Great Testers In Selenium, the and operator in XPath is a logical operator that allows you to combine multiple conditions to locate elements more accurately. An element is matched only when all specified conditions evaluate to true, making your locators more precise and reliable. Syntax The general syntax [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":32175,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[1400,1817,1816,1561,1818,448,403,1560,1559,1795,1794],"class_list":["post-8200","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium-tutorials","tag-ajax-call-in-selenium-webdriver","tag-and-expression","tag-and-operator","tag-axes-in-selenium","tag-or-expression","tag-selenium-webdriver","tag-webdriver","tag-xpath","tag-xpath-contains","tag-xpath-expression","tag-xpath-within-selenium"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=8200"}],"version-history":[{"count":7,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8200\/revisions"}],"predecessor-version":[{"id":32917,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8200\/revisions\/32917"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/32175"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=8200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=8200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=8200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}