The first rule of any technology used in a business is that automation applied to an efficient operation will magnify the efficiency. The second is that automation applied to an inefficient operation will magnify the inefficiency. – Bill Gates
A lot of times when we automate a feature and run it for the first time, it gives us Green result, However, on the second run it gives us the Red results. And after further analysis, we get to know that web elements we are dealing with are dynamic in nature and It becomes quite challenging to interact with such dynamic elements. So, Lets discuss some techniques to handle such web elements while scripting the automation using using any automation tool, be it open-source like selenium webdriver or commercial like UFT/QTP.
What do we understand by Dynamic Web Element?
Dynamic element is that Web Element whose ID’s, actually not only ID’s it can be any attribute like Class Name , Value etc. are not fixed. It changes every time you reload the page. So, you cannot handle that element simply by locator.
For ex: Gmail Inbox elements, there class name gets changed on every login.
Dynamic elements are database-driven or session-driven. When you edit an element in a database, it changes a number of areas on the application under test. Dynamic elements are strictly content, with formatting being laid out in the design. Dynamic identifiers are generally used for text-boxes and buttons
If you are automating a dynamic website, the scripts will break as soon as the content changes which will cause your test to fail. Then you have to update your test case each and every time, which is a tiresome task.
We always need to understand the behavior of these elements as the page is reloaded or entered in new session. Once we understand it, we can devise a strategy to interact with these elements.
Some of the strategies are listed below:
Let’s see dynamic element example with tag as Button which has dynamic ID and Class name,
where ID is getting changed from ‘Hstpl-3465-text1’ to ‘Hstpl-4434-textE2’
and class name gets changed from “Hstpl-Class-text45” to “Hstpl-Class-text73” on every new session.
1. Relative Xpath with Starting Text:
Like partial link selector in selenium, we can also use Xpath search with starting Text match element. We can apply ‘starts-with’ function to access the element as shown below:
2. Relative Xpath with Following or Preceding Node
The following include all the nodes that follow the context node. . We can apply ‘following’ to specify the following elements in the web element list.
//button [contains(@class, ‘Hstpl-Class’)] /following:: input[contains(@id,’Hstpl-’)]
//input [contains(@id,’Hstpl-’)] /preceding:: button[contains(@class, ‘Hstpl-Class’)]
3. Relative Xpath with Text Contains
Few dynamic elements contain static values, On the basis of those values, we can use ‘contains’ function to search such elements. For example, in above html button class name contains static string ‘Hstpl-12345’. We can use XPath to search for a button element with class name containing ‘Hstpl’.
4. Relative Xpath with Multiple Attribute
We can also add more than one condition to search element using Xpath. Like button with ID that contains ‘Hstpl’ plus class that contains ‘text’.
//button[contains(@id,’Hstpl-’)] [contains(@class, ‘Hstpl-Class-text’)]
5. Element with Index
Some times there are more than one elements present in the DOM with similar attributes and it becomes difficult to search them when they are dynamic in nature. For example, there are 10 buttons on page and you want to locate 5th button. Then we search elements with ‘button’ tag and navigate to 5th index of list of buttons to get that element:
If hierarchical level doesn’t get change, then this would be one method to trace dynamic element.
6. Absolute Xpath Method
Absolute Xpath method uses complete path from the Root Element to the particular element. An absolute Xpath starts with html and forward slash (/) as shown below. You can use firepath (firebug) to generate Xpaths. They are prone to more regression as slight modification in DOM makes them incorrect or refer to a different element. Normally it’s not considered as a best practice to use absolute Xpath, However it solves the Dynamic element problem.
7. Using IWebElement Interface.
One more way to handle dynamic element is to find all elements with Tag name and search required element by contains text value or element attributes. For example to search button with specific text you can use below code:
IList webElement = BrowserDriver.GetDriver().FindElements(By. TagName(‘button’));
IWebElement element1 = webElement.First(element => element.Text == “Hstpl”);
** Note: IWebElement interface is used to interact with both visible and invisible elements present on a page.