How to find all the elements in list view.
created
Aug '15last reply
Apr '17- 7
replies
- 22.6k
views
- 6
users
- 1
like
How to find all the elements in list view.
The way I've done this is to find something that is common between each of the items within that list view. A lot of the list views I've encountered contain layouts (e.g. relative layout) and these layouts have a common id. So what I usually do is the following
List nameOfList = driver.findElement**s**(By.id("id of relative layout"));
then just confirm this has worked I usually check the size of that list
System.out.println(nameOfList.size());
I hope this helps.
Hi,
Thanks for your reply. i am able to find all the elements. Got into another issue.
i am able to click only first element in the list when iterate through the list. how can i click all the elements one by one.
For ex: when i open whats app, i can see 3 elements in linear layout as given below
"CALLS" "CHAT" "CONTACTS"
i am able to click calls successfully and not able to click remaining ones. Getting element not found exception.
Following is the text and corresponding xpath which i got when i run the script.
CALLS
[[AndroidDriver: Android on LINUX (f97ff732-a84a-4a48-b45c-0362af37dcdb)] -> xpath: //android.widget.TextView[@resource-id='com.whatsapp:id/tab']]
CHATS
[[AndroidDriver: Android on LINUX (f97ff732-a84a-4a48-b45c-0362af37dcdb)] -> xpath: //android.widget.TextView[@resource-id='com.whatsapp:id/tab']]
CONTACTS
[[AndroidDriver: Android on LINUX (f97ff732-a84a-4a48-b45c-0362af37dcdb)] -> xpath: //android.widget.TextView[@resource-id='com.whatsapp:id/tab']]
since i have same xpath for all three elements, i am able to click only "CALLS". i am trying with below code
List list1=d.findElements(By.xpath("//android.widget.TextView[@resource-id='com.whatsapp:id/tab']"));
System.out.println(list1.size());
System.out.println(s.length);
for(int j=0;j {
System.out.println(list1.get(j).getText());
System.out.println(list1.get(j));
list1.get(j).click();
Thread.sleep(5000);
}
Please help.
Thanks Rjimms & Bhaskar. This working for me & i am able to select required button dynamically by using below code(where i want to select the button whose doctor name == "Dr Gitanjali").
java.util.List list1=dr.findElements(By.xpath("//android.widget.TextView[@resource-id='MediAngels.MediAngels:id/consult_doctor']"));
java.util.List list2=dr.findElements(By.xpath("//android.widget.TextView[@resource-id='MediAngels.MediAngels:id/doctor_name']"));
for(int i=0;i<=list2.size();i++){
for(int j=0; j<=list1.size();j++){
String Expt="Dr Gitanjali";
String Act=list2.get(i)).getText();
if(Expt.equals(Act)){
list1.get(i)).click();
}
}
}List <WebElement> list1=dr.findElements(By.xpath("//android.widget.TextView[@resource-id='MediAngels.MediAngels:id/consult_doctor']"));
List <WebElement> list2=dr.findElements(By.xpath("//android.widget.TextView[@resource-id='MediAngels.MediAngels:id/doctor_name']"));
should provide you the list of WebElements to getText() or click()