Dynamic Elements
In many cases, automation engineers are challenged to address the needs of ever-changing applications and one of the case is dynamic element where the element’s properties are changing frequently due to many factors. If such elements are involved in your automation script then identifying those elements become crucial and there is no direct approach to tackle the situation. Here we have listed down a few strategies which could be used depending on the situation
How to identify dynamic elements in Selenium?
An absolute X-Path is the complete path
which starts from root element and goes until the element we need to identify
/html/body/div[3]/div/div[2]/div/div[1]/div/input
Any path which starts with the
current identified element is referred to as relative path
- Using
contains or starts with function – Example below
//input[starts-with(@id, ’Century’)]
//button[contains(@class, ‘Century’)]
- Using
multiple attributes – Example below
//input[contains(@id,’Century’)] [contains(@class, ‘Century123’)]
- Using
the Preceding or following elements – Example below
//button [contains(@class, ‘Century123’)] /following::
input[contains(@id,’Century’)]
//input [contains(@id,’CITS’)] /preceding:: button[contains(@class,
‘Century123’)]
- Locating
elements using index – Example below
ffdriver.findElements(By.tag(‘input’))[11]
ffdriver.findElements(By.xpath(//*[contains(@id,
‘Century’).get(3)