This site is in read only mode. Please continue to browse, but replying, likes, and other actions are disabled for now.
1 / 8
Aug 2015

How to find all the elements in list view.

  • created

    Aug '15
  • last reply

    Apr '17
  • 7

    replies

  • 22.6k

    views

  • 6

    users

  • 1

    like

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.

Hi,

for(int j=0;j<=list1.size();j++)
{
list1.get(j).click();
Thread.sleep(2000);
}

The above code should work.Let me know if it works , If not please have a try with By.name,className if it has other attributes as well !!

Regards,
Bhaskar.

(Or)

List tabList = driver.findElements(By.id("com.whatsapp:id/tab"));
int n = tabList.size();
for(int i=0;i<=n;i++)
{
tabList.get(i).click();
Thread.sleep(3000);
}

Hope this hepls !!

1 month later

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();
	    		        	}
	        		}
	        	}
3 months later
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()

1 year later