By using get_attribute() method we can get the content description of any mobile element.
First we need to create a variable for the mobile element and assign it to find element method using any locator type.
Now using variable name call get_attribute() method and pass argument as “content-desc", Store the data in any variable and use it for required operation.
Syntax :
ele_id = driver.find_element(AppiumBy.ID,"com.skill2lead.appiumdemo:id/EnterValue")
ele_id.get_attribute("content-desc")
Example : In the below example we are launching the app on an Android device and get the content-description of the button using the ID locator.
Get_Content_des.py
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
import time
# Step 1 : Create "Desired Capabilities"
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['automationName'] = 'UiAutomator2'
desired_caps['platformVersion'] = '10'
desired_caps['deviceName'] = 'Pixel3XL'
desired_caps['app'] = ('/Skill2Lead/Appium_Demo_App/Android/Android_Appium_Demo.apk')
desired_caps['appPackage'] = 'com.skill2lead.appiumdemo'
desired_caps['appActivity'] = 'com.skill2lead.appiumdemo.MainActivity'
# Step 2 : Create "Driver object"
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
ele_id = driver.find_element(AppiumBy.ID,"com.skill2lead.appiumdemo:id/EnterValue")
# Step 3 : Get the content description of the button using ID locator
print("Content Description of a element :", ele_id.get_attribute("content-desc"))
time.sleep(2)
driver.quit()