There several mechanism supported by Selenium to identify the element/object on the browser and to perform some actions. There are times when identifying an element and working with it is a lot difficult. I am sharing one of the problem I faced during identifying an element that is dynamically change after each page load (each new request).
Problem
Web page loads unordered listing element with children listings that have dynamic IDs on each requests/page loads. If you identify an element and try work with selenium scripts the next time scripts run the ID would change and you'll receive dirty exception of NoSuchElementFound blah blah.
Summary outer HTML
This the HTML of the application I was trying to work with.
"listingItem">
g="0" cellpadding="0">
Problem
Web page loads unordered listing element with children listings that have dynamic IDs on each requests/page loads. If you identify an element and try work with selenium scripts the next time scripts run the ID would change and you'll receive dirty exception of NoSuchElementFound blah blah.
Summary outer HTML
This the HTML of the application I was trying to work with.
......
Note:
ID and other selenium selector are not that help since every attribute is same the attributes that are different are dynamic.Therefore, you can not uniquely identify an element and work confidently with.
Inner HTML of the First Listing Element.
This is the detailed HTML of the first listing element as shown above.
PKR 850,000
The value that need to be pick was PKR 850,000 (which is also changing but its position does not change)
Solution
Firepath CSS selector does the trick and help you evaluate your expression, by itself firepath/firebug can give the exact output as below.
#listings li:first-child strong > span
#listings li:nth-child(2) strong > span
...
List goes on and on
If the same action needs to be performed on each element this can be iterate by adding the loop as
for(int i = 4; i <11 div="" i="">
System.out.println(driver.findElement(By.cssSelector("#listings li:nth-child("+i+") strong > span")).getText());
}
11>