Element Selection with conditions

I have to select elements with a condition.

element which is having class value as “android.widget.RelativeLayout” and resource id should be blank. Only these elements should be counted

Actual Problem

I have multiple elements with class as “android.widget.RelativeLayout” i want only few among them. When i observed i have a blank id for desired elements.

When i write below code. It is selecting all the elements:
List e = d.findElementsByClassName(“android.widget.RelativeLayout”);
System.out.println("size of list is " + e.size());

Suggest your inputs techies… please

You can find elements using xpath with condition, resource-id is blank.

List elementList = driver.findElements(By.xpath(“//android.widget.RelativeLayout[@resource-id=’ ']”));
System.out.println(“Total elements :” + elementList.size());

Thanks for your reply. Still unable to get output. Now it is displaying as 0

Can i send you screenshot saved using Uiautomator can you please help me out

@dsraidu yes please. Share the screenshot.

Please find attached screenshots

Please give a try with:


Hey TuHUynH,

I want to store all location elements into list. Your sytax will select only particular element.

Hope you had a clarity on my requirment.

Thanks for taking look

@dsraidu : In this case, you can get elements using ListView instead of android.widget.RelativeLayout. If you don’t want to include index “0” which is “Total Balance”. Add if statement to ignore it.

List<WebElement> elementList = driver.findElements(By.className("android.widget.RelativeLayout"));
    for(WebElement we : elementList) {
       if( !we.getAttribute("index").equals("0")) {
           if(we.getAttribute("resource-id").toString().isEmpty() ) {
               System.out.println("Element with blank resource-id");
1 Like