From Confused to Confident: Master XPath for Web Testing
Introduction Learn how to confidently build, refine, and troubleshoot XPath expressions. This practical, example-driven guide covers TestComplete HTML and CSS basics, XPath syntax, and browser validation techniques, helping you create stable and maintainable automated web tests. While TestComplete can generate XPath or CSS selectors automatically, generated selectors can break if page layouts change, so knowing how to tweak XPath manually ensures your tests remain reliable even on pages that change frequently. HTML Basics W3Schools Reference HTML stands for HyperText Markup Language. It is the standard markup language for creating web pages. An HTML document can be represented as a Document Object Model (DOM), which is a tree of nested elements. Each element may have attribute name–value pairs that provide additional information about that element like its ID or class. An HTML element typically includes a start tag, content, and an end tag, for example: <tagname>Content goes here...</tagname> Save this simple example as a file named "index1.html" and open it in a browser, or paste it in W3Schools Tryit Editor left pane section. The <img> tag’s src may point to any valid image file or a valid URL. <!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <h1>This is a Heading</h1> <p>This is a paragraph.</p> <a href="https://www.w3schools.com">This is a link</a> <p>This is another paragraph.</p> <img src="w3schools.jpg" alt="W3Schools logo" width="104" height="142"> </body> </html> CSS Basics W3Schools Reference CSS stands for Cascading Style Sheets and defines how HTML elements are displayed color, size, spacing, and layout. CSS rules consist of a selector and a declaration block, which contains property–value pairs, for example: h1 { color: blue; font-size: 24px; } Save this simple example as a file named "index2.html" and open it in a browser, or paste it in W3Schools Tryit Editor left pane section. <!DOCTYPE html> <html> <head> <style> body { background-color: yellow; } p { color: red; margin-left: 100px; } </style> <title>Page Title</title> </head> <body> <h1>This is a Heading</h1> <p>This is a paragraph.</p> <a href="https://www.w3schools.com">This is a link</a> <p>This is another paragraph.</p> <img src="w3schools.jpg" alt="W3Schools logo" width="104" height="142"> </body> </html> CSS selectors are different from XPath selectors, and TestComplete can use both depending on the browser engine. XPath Basics W3Schools Reference XPath stands for XML Path Language. It uses a path-like syntax to identify and navigate nodes in an XML or HTML document. Now that we’ve reviewed how web pages are structured, let’s see how XPath helps locate elements within them. In web testing, XPath is commonly used to locate elements within the HTML DOM. Important points to keep in mind: HTML <tags> and element names aren’t case-sensitive are normalized to lowercase, but attribute values are and usually enclosed in quotes. XPath works the same in HTML as in XML and navigates elements using relationships: Ancestors, Descendants, Parent, Children, and Siblings. Absolute Path: Starts from the root with a single slash "/", for example: "/bookstore". Absolute XPath is brittle because page layouts often change. Relative Path: Starts with double slashes "//" and searches anywhere in the document, for example: "//book". Consider this XPath Expression: "//div/a[contains(@class, 'button')]/@href". Let’s break it down step by step: 1. // = Search all descendants in the document 2. div = Select any "<div>" element 3. / = Navigate to direct child 4. a = Select the "<a>" child element 5. [] = Apply a condition 6. contains(@class, 'button') = Select "<a>" elements whose "class" attribute value contains "button". The contains() is very useful for partial matches. 7. / = Navigate to the next level 8. @href = Returns the value of the "href" attribute. In Plain English: Find all "<a>" links inside "<div>" elements with "button" in the class attribute and return their "href" values. Understanding XPath expressions helps handle dynamic pages, where IDs, classes, or structures may change across builds or browsers. You could also use wildcards and parameters. Testing XPath in Chrome & SmartBear Sample Shop Browser Developer Tools (DevTools) among other things are used to debug the HTML DOM and examine individual elements. Press "F12" to open DevTools. When DevTools is docked to the right (the default), the page layout may adjust dynamically. To maintain the original layout, and for this exercise switch the DevTools dock position to the bottom. SmartStore’s dynamic structure may vary and actual results may differ if the site layout changes. The Console panel in DevTools lets you execute XPath expressions directly in the browser to validate their syntax and confirm that they correctly identify elements within the DOM. The $x("...") is Chrome-specific console command function and is also not part of TestComplete. Type your XPath inside the command. Step-by-step example On the SmartStore Page click on the header "Help & Services", then right-click on the menu item "About Us" and select "Inspect". The Elements panel in DevTools will expand and highlight the corresponding HTML DOM element. Perform the following steps in the Console Panel: Type $x("") and notice the console displays "undefined". Type $x("//a") and notice the console displays "(109)" meaning there are 109 matching <a> elements, stored in an array. Type the full XPath $x("//a[span[text()='About Us']]") and notice the status changes to (2) signaling two matches were found. Note: If your target text is not inside a <span>, adjust to $x("//a[text()='About Us']"). Press Enter to execute the XPath expression and then expand the results to hover your mouse over the first entry "0: a.dropdown-item.menu-link", the element in the SmartStore page gets highlighted. Similarly, in the Elements panel, hovering over the element highlights it in the SmartStore page. Right-click on "0: a.dropdown-item.menu-link" and select "Open in Elements panel". This navigates directly to the HTML DOM element in the Elements panel. Scroll the SmartStore page to the bottom, and in the Elements panel, right-click on the element and select "Scroll into view". The SmartStore page will scroll back up and the element on the page will be highlighted. Right-click on the element and select "Copy" > "Copy XPath", "Copy full XPath", "Copy selector", or "Copy Element". You can paste into any editor for review. The "Copy XPath" generates an absolute path, while the "Copy full XPath" is fully qualified from root, both can break if the DOM structure changes. Repeat the steps for the second match "1: a.menu-link" to reinforce your understanding. Refining XPath for precision Option 1: By adding attribute filters with name–value pairs: view the element details and update the XPath $x("//a[span[text()='About Us'] and @class='dropdown-item menu-link']") Option 2: A more robust and preferable approach is to reference an ancestor in the XPath. Copy both elements' full XPath and paste them in a text editor for visual comparison. /html/body/div/div[2]/header/div[1]/div/nav/nav[2]/div/div/div/a[5] /html/body/div/div[2]/footer/div[2]/div/div/div[3]/nav/div/div/ul/li[1]/a The full XPath is a brittle locator that breaks with layout changes. Find the top ancestor and refine the XPath, start from a logical ancestor like “//header//a…” or “//footer//a…” and continue with descendant selectors, not direct children. The double slashes (//) mean any descendant level, not necessarily a direct child. Don’t worry if your XPath doesn’t work the first time—this is normal! $x("//header//a[span[text()='About Us']]") $x("//footer//a[span[text()='About Us']]") When you copy the full XPath from the browser and try to run it, it may not work. Example of a common mistake that will result in such error: "Uncaught SyntaxError: missing ) after argument list". Why: The double quotes (") inside the XPath conflict with the double quotes wrapping the string in JavaScript or TestComplete scripts. Use single quotes (') inside your XPath string to prevent conflicts with the outer double quotes used in the scripting environment. Incorrect: $x("//*[@id="header"]/div[1]/div/nav/nav[2]/div/div/div/a[5]/span") Correct: $x("//*[@id='header']/div[1]/div/nav/nav[2]/div/div/div/a[5]/span") Common Mistakes & Troubleshooting If your XPath expression doesn’t yield expected results, refresh the page or ensure you’re on the correct frame, and check for dynamic rendering (elements loaded after page load). Also try waiting for the element or using relative paths. Check whether the element is inside an iframe — in that case, switch context before applying XPath. Conclusion XPath can initially seem confusing, but with patience and practice, you’ll confidently tackle a wide range of web element identification challenges. Mastering XPath will also help you debug TestComplete object recognition issues more effectively. TestComplete’s flexibility, combined with a solid understanding of XPath, allows you to create robust, maintainable, and reliable automated tests. XPath skills also translate to CSS selectors and other object identification methods, improving cross-technology automation. Start experimenting with simple examples today, you’ll be surprised how quickly manually manipulating XPath becomes your favorite action!Don't update Chome to v140
For those of you that didn't follow this ticket: Chrome minimizes and reopens every on screen action | SmartBear Community When Chome v139 came out TC stopped working with Chome (and edge). This was fixed in TC v15.77.6. Now Chrome updated again to v140, Testcomplete v15.77.6 does not work with Chome again. If you updated Chome already i recommend downgrading Chrome back to v138 or v139.Solved493Views4likes15CommentsObject Recognition Challenges? Help Us Understand Your Use Cases
Hello TestComplete Community! We’re working on a new Visual Object Detection (VOD) capability powered by AI, designed to make it easier to test applications where traditional object recognition struggles. This feature aims to help you reliably identify visual elements that are hard to detect today. To make sure we’re building this in the most useful and relevant way, we’d love to learn more about your current challenges and use cases related to visual or hard-to-recognize objects. Your real-world scenarios will help shape how we design and prioritize VOD. Feel free to share your thoughts by replying directly to this post, or send me a private message if you prefer. Thank you in advance for your feedback we truly appreciate it! Temil Sanchez Product Manager68Views0likes1CommentAccessibility Testing Made Easy: How TestComplete Ensures Web Compliance
Most test automation focuses on functionality but in regulated industries like healthcare, finance, and education, teams must also prove accessibility and compliance. TestComplete’s Web Audit Checkpoints make this simple by integrating accessibility scans directly into automated tests, identifying errors like missing alt text, deprecated tags, and invalid HTML. Teams can set practical thresholds (e.g., zero critical errors, limited warnings) to balance enforcement and flexibility. This ensures every regression run checks not only if features work, but if they meet legal and usability standards. The result is faster compliance, reduced risk, and higher-quality user experiences for everyone. Check out our demo video to see how accessibility testing in TestComplete fits seamlessly into your automation pipeline and helps you build more inclusive, compliant web applications. Accessibility Testing in Testcomplete DemoSupport Current FireFox Versions
We currently are on FireFox Version 128ESR, which was released in July of 2024. When I opened a support ticket, I was informed that you only support 115esr which was release in July of 2023. This means that I'm unable to run automation on FireFox as I don't control what version of FireFox my organization uses.121Views5likes3CommentsButton with "..." not completing selection - step stops after dropdown choice (Chrome & Edge)
Hello, We are experiencing an issue when selecting buttons that open a dropdown menu - specifically the ones with "...". When TestComplete tries to click and select an option from that dropdown, it opens it and seems to pick the right step, but nothing changes afterward. It looks like the action is registered, yet the test does not continue with the next actions. Environment: Chrome 140.0.73399.208 (Official Build) (64-bit) TestComplete 15.76.5 x64 Same behavior reproduced in Edge 140.0.3485.94 (Official Build) (64-bit) Attachments: Run_Test.mp4 - playback shows TestComplete marking the step as passed (green), but the website state does not change. Has anyone seen similar behavior or a configuration/workaround that forces TestComplete to execute the actual interaction (click/selection) instead of resulting in no UI change? Best regards, Aleksandar75Views0likes9CommentsFindAll Method Not Returning Anything
This is hopefully a straightforward solution for someone out there with far more experience than I. My application has rows inside a parent element. Each row is segmented so it also appears to have columns. I need to get all the "add user" buttons that are in a 'column' together in one array and loop through it. Roughly <div> <row 1> <stuff I dont need> <button I do need> <row 2> <stuff I dont need> <button I do need> <row 3> <stuff I dont need> <button I do need> </div> I believe the FindAll or FindAllChildren is the correct route but I am getting empty arrays returned. The buttons all have the same text "Add User" and are SPAN elements (not actually a button element). The buttons are all in the rows as I described and are contained in one parent DIV that wraps around all the rows. (Unfortunately not the column). This is close to what I have without sharing exact code: let column = page.FindElement("//div[contains(@class, 'random-css')]"); Log.Message(column.FindAllChildren("SPAN", "Add User", 300000)) >> returns null Thank youSolved64Views0likes5CommentsWeb recording - multiple chars recorded and mouse-clicks not recorded
To reproduce: [1] Go to https://bearstore-testsite.smartbear.com/ [2] Create a login/password if you don't already have one: MyUserTest5/MyPasswordTest5 [3] Record a new test [4] Enter login/password [5] Click on the Log in button [6] Stop recording Result: The record test will show something like: For Login - "MmmmyUseeeeerTTTTTest555" For Password - "MMMy" Playback is wonky too, like mouse-clicks going to odd places on the page This started on or just before May 5th of this year, 2025: * WebView2 version 136 * On the Edge Browser, and now recently on the Chrome Browser * TestComplete version 15.69 and then 15.77 Windows has continued to update WebView2 and there has been no change of behavior. SmartBear said they reproduced the issue but has not provided a workaround or a fix. I verified my TestComplete plug-in is valid in the browser. I have used TestComplete desktop full-time for 13 years and the Web tool for two years, and love it. They have always had quick fixes for me until now. This one is debilitating. Got any ideas? I'm not seeing many current posts on TestComplete Web. Is anyone even using it????Solved97Views0likes9CommentsParallel testing for web tests
Hi, is there a way to execute Test Complete web tests in parallel within the same machine, by opening the needed browser instances and execute in each one the needed test? For example, I've got test_1 and test_2, both developed for Edge browser, and they're indipendent one from each other from functionality point of view, that is: I could execute them in parallel without any disturbance between both. Is there a way that I can execute test_1 and test_2 with 2 browser instances in the same time? The scope is to save time by half for tests' execution. I'm available to try a solution both through AzureDevOps release pipeline changes or test project changes. I've searched for documentation within Test Complete but I've found for now only information regarding executing parallel tests within different machines, but it is not my scope, since I need to execute them within the same machine. Thanks and regards, SimonaSolved76Views0likes2CommentsChrome minimizes and reopens every on screen action
We're experiencing an issue where for every on screen action Chrome minimizes and reopens. I created a brand new test suite and wrote a simple test navigating around the smartbear site and we are still running into this issue. We can't run tests because every time we open a dropdown it goes away when chrome minimizes and re opens. We've been on TC version 15.75.22.7 for two months with no problems. (Waiting on IT to upgrade TC to latest version to see if that helps) Chrome did just update to version 139.0.7258.67. I have opened a case with support but wondering if anyone else has run into this issue with the latest chrome update. Thanks in advanceSolved1.2KViews7likes45Comments