Wednesday, 4 January 2017

Few things about handling those nasty dynamic elements using Xpath

Have you ever come across a situation where you struggle to find a stable Xpath of a element .I know it's frustrating, It is even more frustrating when elements of your test subject tend to change their paths with each version.Here are some tweaks applied by automation engineers to handle dynamic element and find stable Xpaths.


Scenario1: Dynamic elements with text as innerHTML.

In the below image you can see that the highlighted div has dynamic parents so it is impossible to find a relative Xpath based on the parent element.There are numerous div elements with the same class name on top of that that class name has a space which makes it fairly impossible to find a stable Xpath.
Dynamic Element Xpath in Selenium

How ever you can use the following text() function to match the innerHTML of the div.

Dynamic Element Xpath in Selenium


Scernario 2: Dynamic SVG element.


 In the following picture you can see a SVG element which has a dynamic class name.How to handle that.
Dynamic Element Xpath in Selenium

 In my scenario , the position of <g> element is at 9th.However this might change with each release of the product I am testing.How to handle this kind of situation.For this you can use the contains function like following.


Dynamic Element Xpath in Selenium







Scenario 3:Multiple elements with same xpath

In the following picture the inputs has similar relative Xpath.
Dynamic Element Xpath in Selenium


You can use the (..) operator for handling this kind of situation. As the label text is constant for each elements.
Dynamic Element Xpath in Selenium




Thanks I hope this helps you.


























No comments:

Post a Comment