Get value of content-desc


#1

how to get value of content-desc where multiple elements id or class name is same, can anybody reply with syntax, Please.


#2

try xpath
//android.view.View[@content-desc=‘the content’]

or if u need the value then
try getAttribute(‘content-desc’)


#3

Hi Mayuresh,
Thank you for your reply, i tried both way but unable to get the value, it says:
An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)

String a=dr.findElement(By.className("//android.view.View[@content-desc=‘Abscess Overview Heading’]")).getAttribute(“content-desc”)//also tried getText().;
System.out.println(a);

there is an option in UiObject, where there is a method to get the value of content description, but don’t know the syntax, you help is highly appreciated, Thanks.


#4

try to get content-desc using attribute name so
instead of
driver.findelement(By.id(“id”)).getAttribute(“content-desc”)
try this
driver.findelement(By.id(“id”)).getAttribute(“name”)

i am able to get !!


#5

getAttribute(“name”) works for me


#6

Hi,

I am facing same issue and able to resolve by driver.findelement(By.id(“id”)).getAttribute(“name”).
But my question is why can’t we read cont-desc like this
driver.findelement(By.id(“id”)).getAttribute(“content-desc”)

Is it a bug or any other logic? I need to understand this issue in depth :slight_smile:


#7

@Sheetal_Singh, why don’t you use uiautomator locator strategy?


#8

Hi Mayuresh, I’m using Appium + Ruby to test Android. Since I can’t use find element by name anymore for Appium 1.5.3. How can I find the following?

#resource-id: result
#content-desc: completed

I’ve tried xpath //android.view.View[@content-desc=‘result’] but I’m getting a syntax error.

Thank you for any help!!


#9

Try this , let me know if it works.
//android.view.View[contains(@content-desc,‘completed’)]
//android.view.View[contains(@resource-id,‘result’)]


#10

Hi Mayuresh, it works like a charm!!! Thank you so much!!!


#11

Welcome!! :slight_smile:


#12

See this issue for an explanation why some XML, elements, and attributes can be compressed away: https://github.com/appium/java-client/issues/74#issuecomment-50886465

The reason why you cannot use getAttribute() to grab the"content-desc" value from the element is because Android UIAutomator has the option of using either a full UI XML dump or a compressed dump, decided from the start of the UIAutomator process. Appium is using the compressed dump, so while you can find any element or attribute with an XPATH call, once you have an element you can only get a subset of attributes dynamically.

getAttribute can retrieve a subset of the values, including “text”, “clickable”, “checkable”, and others…
The attributes that cannot be grabbed even though they are available in the full UI XML dump are “index”, “content-desc”, “class”, and others.

It appears that there is no way around this unless we specify UIAutomator to ALWAYS do a full UI XML dump from the beginning of the process, which would slow down the automation. This user restarts the UIAutomator server multiple times to achieve this: https://github.com/appium/java-client/issues/74#issuecomment-64672526

If anybody else has an idea about how to dynamically grab getAttribute(“content-desc”) without relying on an XPATH call (how to query against the full XML), please post in a reply how to achieve this!


#13

Like it was said before, you can use:

element.getAttribute("name")

But this is a poor implementation and in UIAUtomator2 it does work anymore. So better is to always use:

element.getAttribute("contentDescription")

#14

its working fine

i have issue with get text method i want text from the conent-Desc how can get the text


#15

This works in c#
driver.FindElement(By.ClassName(className)).GetAttribute(“value”);


#16

hi
today i tried gettext method i got the text from content-desc
webelements properties text id class content-desc
if u want content-desc must should empy in text field then
only we get the text from content-desc otherwise we get the
text from text field ex i tried amazon app

this is amazon icon we get the text from content-desc

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement se=driver.findElement(By.id(“in.amazon.mShop.android.shopping:id/action_bar_home_logo”));
System.out.println(se.isDisplayed());
System.out.println(se.getAttribute(“name”));


#17

Hey this worked for me.

element.getAttribute(“contentDescription”)

Thank You


#18
xpath = '//android.widget.Image[@content-desc="profile_pic"]'

#19

element.getAttribute(“contentDescription”)

This worked for me, thank you.


#20

None of the below is working for me. getting null value though value is available for content-desc attribute. Any solution?

getAttribute(“content-desc”)
getText()
getAttribute(“contentDescription”)
getAttribute(“name”)