Troubleshooting Appium: Accessing the “content-desc” Attribute

Troubleshooting Appium: Accessing the "content-desc" Attribute

5 April 2024 Stephan Petzl Leave a comment Tech-Help

When automating mobile applications using Appium, developers often need to interact with various elements within the app. A common operation is to retrieve an element’s attributes, such as “content-desc” in Android applications, which provides a description of the element’s content for accessibility purposes. However, some Appium users encounter difficulties when trying to access this particular attribute. This guide aims to provide a solution to this issue.

Understanding the Issue

The problem arises when using Appium to locate elements by their class name and then attempting to manipulate data based on each element’s “content-desc” attribute. An error is thrown, indicating that the attribute cannot be retrieved:


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);
}

        

The error message typically looks something like this:

Found arrayOfProperties2 total: 32

org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters.

Proposed Solution

To address this error, instead of using “content-desc”, use the “name” attribute to retrieve the description data. Here’s how you can adjust your code:


for (WebElement property : arrayOfProperties2){
    String contentDesc = property.getAttribute("name");
    if (contentDesc.contains("property"))
        propertyMarkerEle.add(property);
}

        

By making this change, your code should be able to successfully retrieve the description data of each element without throwing an error.

Additional Information

If you are curious about the different attributes available for retrieval in Appium, here’s a quick rundown:

Boolean Attributes:

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

String Attributes:

  • contentDescription
  • text
  • className
  • resourceId

Note that these attributes may vary depending on the language bindings you are using with Appium, and it is always good practice to refer to the official Appium documentation or the binding-specific guides for the most accurate and up-to-date information.

Conclusion

Accessing element attributes is a fundamental part of automating tests with Appium. When faced with errors related to attributes, it’s important to understand the alternative attribute names that may be applicable. In this case, replacing “content-desc” with “name” provides a simple and effective solution. As with any automation task, thorough testing and validation are key to ensuring your scripts work as intended.

Like this article? there’s more where that came from!