7

In the below case, Appium is correctly able to locate elements by class, but when we want to manipulate the data based on each element's content-desc we see an error. Why can't we get attribute for content-desc? Any advice appreciated.

    List<WebElement> arrayOfProperties2 = driver.findElementsByClassName("android.view.View");
    List<WebElement> propertyMarkerEle = new ArrayList<>();

    System.out.println("Found arrayOfProperties2 total: "+ arrayOfProperties2.size());

    for (WebElement property : arrayOfProperties2){
        String contentDesc = property.getAttribute("content-desc");
        if (contentDesc.contains("property"))
            propertyMarkerEle.add(property);

Error: Found arrayOfProperties2 total: 32
org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)

HRVHackers
  • 2,793
  • 4
  • 36
  • 38
  • This can help I guess https://github.com/appium/appium/issues/5142. I believe the code changes are pushed in. – Naman Feb 03 '16 at 12:30

4 Answers4

17

Use "name"

property.getAttribute("name");

TinyTimZamboni
  • 5,275
  • 3
  • 28
  • 24
  • Hi, driver.findElementByXPath("android.widget.TextView[]").getAttribute("name"); is not getting attribute text from app.. can you tell me where i did wrong in code.. class ,package and content-desc not printing, but remaining node detail print successfully.. i don't know what is the wrong .. Thanks for advance – Kv.senthilkumar Sep 28 '14 at 17:01
  • it might not have a content description then?? – TinyTimZamboni Sep 28 '14 at 23:15
  • 1
    Post a screenshot from Google automator viewer showing all the attributes – HRVHackers Sep 29 '14 at 18:22
  • I have tried like : driver.findElementByXPath("android.widget.TextView[]").getAttribute("content-desc"); but unable to get content-description details even has the content-desc on app element.. i could not able to post app screenshot due to policy restriction. remaining node details print successfully. – Kv.senthilkumar Oct 20 '14 at 11:08
  • Not sure. When you view the xml source, is the content descriptions visible there? – TinyTimZamboni Oct 21 '14 at 16:40
  • Works for me, thanks! It's weird because in UIAutomatorViewer the value does appear under "content-desc" instead of "name". – Pupper Jul 29 '16 at 03:01
  • @TinyTimZamboni, would you be kind enough to help me understand to get to a solution for this http://stackoverflow.com/questions/39484982/access-toggle-button-in-android-settings-using-appium-whlie-client-is-written-in – Pankaj Nimgade Sep 14 '16 at 07:55
  • @S200 the correct string to get content-desc is `contentDescription`. See my answer below. Why `name` works too is because it [falls back](https://github.com/appium/appium-android-bootstrap/blob/master/bootstrap/src/io/appium/android/bootstrap/AndroidElement.java#L185) to `contentDescription` if `text` is empty – ibizaman Feb 19 '17 at 07:56
1

The list of accepted attribute names can be found in boolean attrbutes and string attributes.

The boolean attributes are:

  • enabled
  • checkable
  • checked
  • clickable
  • focusable
  • focused
  • longClickable
  • scrollable
  • selected
  • displayed

The string attibutes are:

  • contentDescription
  • text
  • className
  • resourceId

I tested this using the python bindings.

Credit goes to TikhomirovSergey in a github comment.

ibizaman
  • 3,053
  • 1
  • 23
  • 34
-1

text can be used as

driver.findElement(By.xpath("//*[@text='Remove Contact']"))

OR content-desc can be used as

driver.findElement(By.name("Remove Contact"))

driver.findElement(By.xpath("//*[@content-desc='Remove Contact']"))

driver.findElement(By.name("Remove Contact"))
Naman
  • 27,789
  • 26
  • 218
  • 353
-1

try this:

driver.findElement(By.AccessibilityID(""));

CLuhring
  • 1
  • 1