Yes. There is a way to write a robust XPath locators: robust against the possible changes in the target HTML. See the following page of Katalon doc:
| Method | Description | Example |
|---|---|---|
| Checking Multiple Attributes | Add more than one condition to search element using XPath. | //*[@id=‘btn-login’][@type=‘submit’] |
| Index | Specify a given tag name in terms of the index value you wish to locate. Use this when more than one element is present in the DOM with similar attributes, and it becomes difficult to search for them. | //div[@class=‘form-group’]//select[1] |
| Chained XPath | Use multiple XPath expressions and chain them. | //section[@id=‘appointment’]//span[@class=‘glyphiconglyphicon-calendar’] |
| Contains() | Use the Contains() method to detect dynamic elements that contain static values. | //button[contains(@id, ‘btn-login’)] |
| Last() | Last() is a method used in an XPath expression. It is used to get the very last node. | //ul[@class=‘list-inline’]/li[last()] |
| Start-with() | The Start-with method finds the element using the starting text of an attribute. This method is useful when the first part of the attribute value is fixed (static), and the rest is dynamic. | //h3[starts-with(text(),‘We Care About’)] |
| Preceding | Select all nodes that come before the current node. | //*[@id=‘btn-login’]//preceding::input |
| Following | Select all elements in the document following the current node( ). | .//*[@id=‘txt-username’]//following::input |
| Following-sibling | Select the following siblings of the context node. | .//*[@id=‘txt-username’]//following::input |
| Ancestor | Select all ancestors (parent, grandparent, etc.) of the current node. | //ul[@class=‘list-inline’]/ancestor::div |
| Child | Select all children of the current node. | //ul[@class=‘list-inline’]/child::li |
| Descendant | Select all descendant (child node, grandchild node, etc.) of the current node. | //ul[@class=‘list-inline’]/descendant::li |
Unfortunately, Katalon’s tools (Record Web, Spy) are not capable to generate those “robust XPath” right away. The tools simply have no idea what is the robustness of a locator against a given HTML. You need to train your skill (of XPath or CSS Selector) and you should manualy rewite the auto-generated locators to the robust ones. If you could do the rewrites, you would free yourself from the maintenance nightmare.
No. I am sure Katalon’s tools can not do the rewrite jobs automatically for you.
I am not sure how much the AI in the world is capable (trained) about rewriting the flaky locators to the robust ones against a given HTML. I don’t know if anyone in the industry is working on trainging AI so that it is capable of that sort of work. Is Katalon working on it?
You need to learn the XPath technology by reading tutorials and documents; go through sufficient amount of exercices where you manually rewrite the auto-generated locators to robust ones. I believe that you want to learn the XPath technologies as people did in 1999 when the RFC of XPath 1.0 was first published.
I would rather recommend using CSS Selector instead of XPath. See