Getting selected text for a combobox in Android

Android Version : 4.4.2
Appium Server : 1.2.0
Java Client : 1.6.0

I am able to access the combobox and select text from the dropdown list.
In UI it displays selected text.
But programmatically, I am not able to access it via combobox.
If I try, getText() api on WebElement mapping to this combobox, it returns “”. [empty]
Even in UI Dump View, it displays text field as empty.

Currently text si associated with a textview within combobox. How do I access it?

Do I need to access that textView and then getText() ?

I got this working with below code -
driver.findElementByAndroidUIAutomator(“new UiSelector().<<locator>>”).childSelector(“new UiSelector().className(“android.widget.TextView”)”);

I’m new to this appium tool. I have followed the same as above code but its not working for me.
My application having only views (resource id is restricted in my application) . I want to get the text of selected one.
I’m using android 4.4.2 version.
This is my code:

driver.findElementByAndroidUIAutomator(“new UiSelector().className("android.view.View").index(0).instance(1).childSelector(new UiSelector().className("android.view.View").index(1))”);

UI Automator Snapshot:

Your help is highly appreciated !! Thanks in advance.

Hi @PRABU90,
I see that its inside a WebView [android.webkit.WebView], so add some delay to make sure WebView is loaded and then refer to it using content-desc value “Create”

I have following code working for similar situation -

 //If can be omitted, just to make sure below code executes only after webview is loaded.
if(dUtil.isElementFoundByClassName("android.webkit.WebView")) {     
  MobileElement ele = dUtil.findElementByAndroidContentDescription("Create");
		if(ele != null)
		  	ele.click();
  } 

Below two APIs are defined inside DriverUtil class which is referred above as dUtil -

public boolean isElementFoundByClassName(String using) {

    boolean found = false;
    
    if (!mDriver.findElements(By.className(using)).isEmpty()) {
    	System.out.println("Element with Class Name : "+using+" Found!!!");
        found = true;
    }
    else 
    	System.out.println("Element with Class Name : "+using+" NOT Found!!!");

    return found;
}

public MobileElement findElementByAndroidContentDescription(String desc) {
	String using = "new UiSelector().description(\""+desc+"\")";
	MobileElement mElement=null;
	try {
		mElement = (MobileElement) mDriver.findElementByAndroidUIAutomator(using);
	}
	catch(NoSuchElementException e) {
		System.out.println("NoSuchElementExecption : "+e.getMessage());
		mElement = null;
	}
	catch(Exception e) {
		System.out.println(e.getMessage());
	}
	return mElement;
}

Hi @RamS

I have tried with delay but it didn’t work out. I have followed the code above to click the element, but I’m getting a message in console like “Element with Class Name : android.webkit.WebView NOT Found!!!”. Thanks for your reply. I want to get the text of the element. Any idea regarding this ?

Hi @PRABU90
I will try to see if there is any alternative.

But, UIAutomator Snapshot you attached clearly shows this component inside “android.webkit.WebView [0,138][720,1184]”

Can you provide more details like -
Appium Version
Java Client Version
Android Device/Emulator

Hi @RamS: This is working fine with latest appium version 1.3.1… Thanks a lot…

1 Like