Handling Dynamic Elements in Selenium WebDriver

Dynamic Elements WebDriver Automation TesterLogic

While automating any web application using any automation tool, be it open-source like selenium webdriver or commercial like UFT/QTP. we have to identify locators for elements which we need to interact with. It could be ID, Name, CSS Selector, XPath or combination of all these. It is quite straight forward to identify locators for static elements which are clearly defined with static IDs. But in some applications, we come across dynamic elements whose ids/xPaths change every time page reloads. It becomes quite challenging to interact with such dynamic elements.

A common example is listing of products on an E-commerce application. In this example, locators associated with different products can change if page is reloaded after use of sort, filter or add product function. Lets say we want to interact with “Purchase buttons” associated with different Products. These buttons could have IDs in following format.

In this test scenario, we know that element IDs can change on page reload. We can also see that id for each button has a common text “submit-” and a number that increments by value of 1. So, we need to understand our scenario requirements and also understand the behavior of these elements as the page is reloaded. Once we understand it, we can devise a strategy to interact with these elements.

In below, I have listed few common techniques used to handle dynamic elements. They might not work in every scenario, but will give you some idea on possible ways to deal with these elements.

1. Absolute XPath

Xpath Position or Absolute Xpath are most frequently used to resolve the dynamic element issues. Reason is, it does not change with page reloads. However, problem with using Absolute XPath locators is that they are very fragile. They are most prone to breakage in case of change in web page. This could  create a big trouble for us in maintainance, if we start using these frequently, especially in large test suites. Therefore they should only be used as last option. Below is an example of Absolute XPath and XPath Position.

2. Identify Element by starting Text

If the dynamic elements have a definite pattern to them, then we can also use JavaScript functions like “starts-with” or “contains” in our element locators to separate the dynamic part of locator from static part.

For example, in case of dynamic submit button Id example which we discussed earlier, we can apply ‘starts-with’ function to access this locator irrespective of its dynamic part.

3. Identify Element by containing Text

Similarly, in some scenarios where dynamic element is surrounded by a static value, we can use ‘contains’ function. For example we have following element locators…

As we can see ‘usefield’ part of element is static, so we can apply ‘contains’ function to access this element locator as shown below…

4. Identify Element by Index

If there are multiple elements present on page with same locator then we can use following Java code in our selenium WebDriver script to interact with element found at particular index.

5. Identify Element with reference of a closest stable element

We can use the DOM structure to find the closest stable element first and then this stable element can be used as a reference element to find the required element.

DOM structure could be found using Firefox extension like Firebug and FirePath. But in complex and large applications this approach is difficult to use because of large DOM structure.

6. Identify Element by stable preceding Text

For web elements like text field and text areas we can identify them by using the stable text labels nearby. This approach might not be possible in all scenarios but it does resolve the dynamic element issues where possible. Example of this approach is shown below.

End Note

These are only some of the approaches that I have come across and used in different Selenium WebDriver test automation projects to handle dynamic and complex element locator scenarios. Have you ever faced similar scenarios while automating? How did you handle it? Have you any suggestions for me or for our readers! Please share your thoughts and experiences in comments section.
Arif Masood