Andy Williams
Andy Williams

Reputation: 907

Retrieving a list of WebElements and Identifying them

Is there a way to Identify and assign WebElement Names to a list of WebElements? For example using the following convention:W

@FindBy(xpath="")
WebElement listFirstObject;

@FindBy(xpath="")
WebElement listSecondObject;

Forgive me if this is an elementary question, but it seems like it would be a pretty common scenario for any tester as many applications have lists of objects with common names. In my case I have a control list with well over 700 objects and it would be nice to be able to write some iterative method to capture and individually create each common WebElement from the list.

** I have made Updates to my question for further clarification** Taking an entire grid of info is completely new to me so please be specific with the answers as I am trying to understand the logic behind it.

So I have elements I am looking for are Grid Data, I have successfully captured the entire Grid, for example

@FindBy(id="ctl00_SomeGridData_ucControlList_trgControlList_ctl00")
List<WebElement> someGridData;

If I were to individually capture each new addition to the grid it would look as such:

@FindBy(id="ctl00_SomeGridData_ucControlList_trgControlList_ctl00__0")
WebElement someGridObj1;

@FindBy(id="ctl00_SomeGridData_ucControlList_trgControlList_ctl00__1")
WebElement someGridObj2;

@FindBy(id="ctl00_SomeGridData_ucControlList_trgControlList_ctl00__2")
WebElement someGridObj3;

As you can see each individual grid element ends with "__#" Obviously this is an infinite list and I cannot capture every WebElement individually and assign a WebElement value to use for testing. What I am trying to ask is how do I capture this entire list and then if i need to call an individual WebElement later to test how do I do so? I hope this clarifies and thanks for reading.

Upvotes: 1

Views: 4676

Answers (2)

Rajnish Kumar
Rajnish Kumar

Reputation: 2938

Yes you can do this like below:

List<WebElement> mylist = driver.findElements(By.xpath("ur xpath"));
System.out.println("Size of the list is : " +mylist.size()); 

This will give you how many webelements are inside your mylist.

Now apply for loop to print the value of the mylist on console and also in the for loop you can assign them a value like this

for(int i = 0;i<mylist.size();i++){
   System.out.println("index of webelements in increasing order is : " + i + mylist.get(i).getText());  

    // this will assign an index of 0 to max for the weblements in the 
    list like index 0 = for 1 webelement ,index 1 = for 2nd webelement ,
    index 2 = for 3rd webelement and so on 
}

Upvotes: 0

David Baak
David Baak

Reputation: 934

Ok, now that have edited your question, it's clear what you want to know. Assuming your grid data is in td elements, you can do the following.

@FindBy(css = "td[id^=ctl00_SomeGridData_ucControlList_trgControlList_ctl00__]")
List<WebElement> allGridData;

By using the ^ (which means starts with) you collect all the td elements that have an id starting with ctl00_SomeGridData_ucControlList_trgControlList_ctl00__.

Upvotes: 3

Related Questions