I am currently working on a project where the xpath of the elements changes continuously. At the UI display level there is not much visible change in UI, however the DOM elements changes continuously.
I am thinking of a better approach to handle failures in test case due to minor changes in DOM structure. I am currently using Selenium with TestNg framework for UI Automation Testing.
Any suggestions or directions on alternate approach would be helpful.
Question posted in Artificial Intelligence
ChatGBT is becoming a world-wide phenomena, try it out here.
ChatGBT is becoming a world-wide phenomena, try it out here.
3
Answers
My suggestion would be:
1. Try using different locators for particular element[https://api.jquery.com/multiple-selector/%5D
something like selector1, selector2, selectorN. If selector1 is not available in DOM, control will not throw an error instead, it will search selector2 and so on
2. Use Explicit waits
Are you following the orders of locators suggested by Selenium contributors: if not then please follow this order :
Note : Most of the time a cssSelector can replace Xpath, However Xpath has its own advantages which cssSelector do not provide.
For more reference you can go through this SO Link : Css vs Xpath
I use By.CSS selectors rather than by xpath, as these are less prone to changes in the DOM.
So for this example dom:
These selectors work:
Example 1. Find Button, element type and class
Example 2. Find Button, direct child rather than descendent
Uses full DOM and thus form element:
Doesn’t use form element:
Example 3. Find Input following the username label, attribute with value then it’s sibling (+)
Example 4. Find Password input and Button, OR’d (comma) returning more than one element
Example 5. Find username input, has class attribute, but not attribute name = ‘password’:
Another thing I do is use a more generic pattern, which finds multiple items. I’ve found the items are always found in the same sequence and thus I can access the one I want by just using the array index (like in example 4).
Finally, if the DOM has dynamic values I’ve found that I can use a specific stable pattern to find a parent element which has the same dynamic value, thus extract it and then reuse it to find the other element I actually want.