Get Text Method

To get the Text on any required element we need to call the “text” property on Mobile element variable.

First we need to create a variable for the mobile element and assign it to find element method using any locator type.

Using this variable we need to call the “text” property and do the required operation.

Syntax :

ele_id = driver.find_element(AppiumBy.ID,"com.skill2lead.appiumdemo:id/EnterValue")
ele_id.text



Example : In the below example we are launching the app on an Android device and get the text on the button using the ID locator.

  • Launch the App on an Android device.
  • Click on the button using ID locator
  • Get the text on button using ID locator
  • Wait for 2 seconds
  • Close the App

GetTextEx1.py

from appium import webdriver
import time

# Step 1 : Create "Desired Capabilities"
from appium.webdriver.common.appiumby import AppiumBy

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 text of the button using ID locator
print("Text on the button type1 :", ele_id.text)

time.sleep(2)
driver.quit()

We can get the text on the button element by using get_attribute() method as well.

Syntax :

ele_id = driver.find_element(AppiumBy.ID,"com.skill2lead.appiumdemo:id/EnterValue")
ele_id.get_attribute("name")
ele_id.get_attribute("text")

Example : In the below example we are launching the app on an Android device and get the text on the button using the ID locator.

  • Launch the App on an Android device.
  • Click on the button using ID locator
  • Get the text on button using ID locator
  • Wait for 2 seconds
  • Close the App

GetTextEx2.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 text of the button using ID locator
print("Text on the button type-2 :", ele_id.get_attribute("name"))
print("Text on the button type3 :", ele_id.get_attribute("text"))

time.sleep(2)
driver.quit()