Please help me in detecting highlighted child element in linear layout for android

Hi,
I am beginner in appium , trying to automate one android app.
I am trying to find element in linear layout. Please help me how to find linear layout child elements.

Thanks,

Hi. You can find child elements like this:

driver.findElementByClass("android.widget.ExpandableListView").findElementsByClass("android.widget.LinearLayout").get(1)

You can use whatever method you want to locate parent element. To locate child element, i made a list and took second element.

// Provisional code based on screen shot

List[WebElement] parent = driver.findElements(By.className(“android.widget.ExpandableListView”);
List[WebElement] child = parent.findElements(By.className(“android.widget.LinearLayout”);
System.out.println("No of elements in linear Layout class " + child.size());

for (int i=0;i<child.size();i++{
System.out.println("Linear Layout text " + child.get(i).getText());
}

** change syntax of declaring list as less then and greater then sign are not visible in this text area

Thanks Alexis ,

I tried with your code,Its giving error org.openqa.selenium.WebDriverException:

My code is:
Driver.findElement(By.name(“India”)).click();
Driver.findElement(By.className(“android.widget.ExpandableListView”)).findElements(By.className(“android.widget.LinearLayout”)).get(1);

Driver.Close();

1 Like

Thanks amitjaincoer191,

I tried with your code,it shows error FindElementBy is undefined for type List Webelement
My code is
System.out.print(“In Test”);
List parent = new ArrayList() ;
List child = new ArrayList() ;
Driver.findElement(By.name(“India”)).click();
parent = Driver.findElements(By.className(“android.widget.ExpandableListView”));
child = parent.findElements(By.className(“android.widget.LinearLayout”));
System.out.println("No of elements in linear Layout class " + child.size());
Driver.close();

Sorry the code was wrong. Can u use uiautomatorviewer for inspection that is much better for android instead of appium inspector. If you post screen shot of that u can get much reliable code from group members.

Assuming class “android.widget.ExpandableListView” is appearing only 1 time in page if not we can use loop for that also.

List[WebElement] parent = _driver.findElements(By.className(“android.widget.ExpandableListView”));
List[WebElement] child = parent.get(0).findElements(By.className(“android.widget.LinearLayout”));
System.out.println("No of elements in linear Layout class " + child.size());

	for (int i=0;i<child.size();i++){
			System.out.println("Linear Layout text " + child.get(i).getText());
	}

Usually inside linear layout direct text will not be there , we have more classes inside it like EditText,TextView etc …

Run uiautomatorviewer its a batch file from folder
adt-bundle-windows-x86-20140702\sdk\tools\uiautomatorviewer.bat using cmd
get u r device listed through adb and run app in it and click icon next to folder in header and u will get good view of u r app

Now that you have a resource-id, you can just do:

Driver.findElementById("com.cleartrip.android:id/buses_layout");

Provided you want the element highlighted in your last post.

here resource id is exposed so u can find elements by id and loop it over to find text/element u r looking for
Based on info u provided a specific locator cannot be told or post screen shot of exact element u want to locate

List[WebElement] parent = driver.findElements(By.id(“com.cleartrip.android:id/buses_layout”));
for getting first child
parent.get(0).findElements(By.some locator strategy)
parent.get(1).findElements(By.some locator strategy)

it’s good u have hidden package but it’s still exposed in resource id. Clear trip app is on google play store any one can use it and view its element using uiautmatorviewer

Is this a problem ? Anyone can get the app’s package name using dumpsys window with adb shell.

Thanks for reply, there was no bad intention behind hiding app name, i did not know whether we can actually use such app for practice purpose and just habitual of not to disclose such thing in public. I realized it after upload screenshot :smile: