Use cases:
- Handle dynamic list – object id changes based on the number of rows.
- Better validation options for list of objects in one screen.
- Find elements where the relative XPath is different across devices.
- Getting dynamic data from screen.
Solution:
In selenium you can get list of objects with specific property and manipulate it in the code.
For example:
I would like to get the cars category list and number of car in each category.
How to do it:
- Find the common property:
In this example, all the items has the same class property : ui-link-inherit
- In the code use findElements command with XPath “contains” option with the specific property.
List<WebElement> objList = webDriver.findElements(By.xpath(“(//div[@id=\”Content-\”])/ul/li/following::*[contains(@class,\”ui-link-inherit\”)]”));
- Loop over the WebElemet list to work with the objects
The Xpath:
In this example I need to set an anchor to the list as part of my XPath because the page contains other object with the same class property which I want to exclude.
Part 1: The first part is setting the anchor for the elements I looking for:
//div[@id=\”Content-\”])/ul/li/following::
Part 2: the second part is getting all the objects with the calss ui-link-inherit
*[contains(@class,\”ui-link-inherit\”)
Code example:
https://github.com/perfectomobile/examples/blob/master/getObjList.java