# The Power of XPath in Test Automation source: https://developer.mastercard.com/blog/the-power-of-xpath-in-test-automation/index.md Authors: Jakub Kurtiak. In UI test automation, finding elements on a webpage is fundamental and often the source of either test stability or constant test failures. Whether you're working with Selenium, Cypress, or Playwright, you generally choose between: CSS Selectors -- Fast, readable, and great for simple hierarchies like .button.primary or div.card \> h2. XPath -- Exceptionally powerful and flexible. Unlike CSS, it allows you to navigate both up and down the DOM, and use expressions like contains() and normalize-space(). ID/Class/Tag Name/Name -- Simple and efficient, but often unreliable due to dynamic UI updates. Real-World Example: Mastercard Developers Portal As part of my work at Mastercard Developers, I often deal with automating UI tests for our public-facing portal. One interesting challenge is locating elements dynamically across multiple services listed on the Products page. Each service has two key links, one for the Product Page and another for Documentation. If you want to build resilient tests that work across all services, how do you find a universal locator? Simple Case: Product Page Link Let's say we want to click the BIN Lookup product card: XPATH: //h5\[text()='BIN Lookup'\] Straightforward and clean. More Complex Case: Documentation Link Now try finding the Documentation link for BIN Lookup: XPATH: //h5\[normalize-space()='BIN Lookup'\]//parent::a//ancestor::div\[@qa-testid='product-card-wrapper'\]//a\[contains(text(),'Documentation')\] It works but it's long, fragile, and closely tied to the DOM structure. Even a small change to the HTML could break this locator. Making It Cleaner: Collaborate with Frontend Devs The best long-term solution? Talk to your frontend team and ask for a test-specific attribute, like: HTML: Documentation Now your XPath becomes: XPATH: //\*\[@qa-testid='service-doc-link'\] This is short, readable, and dependable. More importantly, the team knows this attribute is critical for test automation and shouldn't be removed or changed. Reusable Selenium Function To make your automation code more dynamic, use a helper function. For example, in Selenium with Java: public void clickServiceDocumentation(String serviceName) { String xpath = String.format( "//h5\[text()='%s'\]/../../../..//a\[@qa-testid='service-doc- link'\]", serviceName ); driver.findElement(By.xpath(xpath)).click(); Call it like this: JAVA: clickServiceDocumentation("BIN Lookup"); And you're done, one function works for every service. Other Complex Examples Explained API Documentation Page Multi Link XPATH: //h5\[normalize-space()='%s'\]//parent::a//ancestor::div\[@qa- testid='product-card-wrapper'\]//a\[contains(text(),'Documentation')\] Finds the documentation link for any given service name. Team Member Status XPATH: //table\[@qa-testid='team-screen-table'\]//child::tr\[@class='align- middle'\]//child::td\[text()='%s'\]/..//td\[3\] Finds a row for a specific team member and grabs their status (third column). Add Production Client Encryption Key Link TH: //h2\[text()='Production'\]/following::span\[starts- with(@class,'add-key')\[contains(@data-uid,'encryption-client')\] Targets a key-related link after a header called "Production". Key Takeaways At Mastercard Developers, we use many types of locators --- but our guiding principles are: Keep them short and stable. Partner with developers to add test-specific IDs and attributes. Refactor brittle locators early to avoid long-term maintenance debt. Use universal, parameterized functions to simplify test code and improve readability. XPath can look intimidating, but when used correctly, it gives you unmatched power to navigate and automate modern, dynamic web applications.