Identifying the MobileElement by “text ”. It takes String as an argument. If an element isn't identified it throws an exception as No Such Element Exception.
Here we use the Android Uiautomator method to find the element by using text locator type.
We can pass value in android uiautomator method in two ways.Let us discuss one by one.
First type , By using the UiSelector() method inside the android uiautomator we can identify the mobile element.
Syntax :
driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,'new UiSelector().text("Locator_Value")')
Second type , We can mention the locator type and pass the value as shown below.
Syntax : driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,'text("Locator_Value")')
Type-1 , Let us see type 1 how to access the button using the text locator.
Example: In the below example we are launching the app on an Android device and clicking on the button using a text locator.
ByText_Type1.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) # Step 3 : "Click on the button using text locator value" ele_text = driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,'new UiSelector().text("ENTER SOME VALUE")') ele_text.click() # Step 4 : Wait for 2 seconds time.sleep(2) # Step 5 : Close the driver object driver.quit()
Type-2 , Let us see type 2 how to access the button using the text locator.
Example: In the below example we are launching the app on an Android device and clicking on the button using a text locator along with UiSelector() class.
ByText_Type2.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) # Step 3 : "Click on the button using text locator value" ele_text = driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,'text("ENTER SOME VALUE")') ele_text.click() # Step 4 : Wait for 2 seconds time.sleep(2) # Step 5 : Close the driver object driver.quit()