By Index Value

Identifying the MobileElement by “Index” number. 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 Content-description locator type.

Syntax :

driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,"UiSelector().index(4)")


Content-Description Locator

Example: In the below example we are launching the app on an Android device and clicking on the button using an index value along with UiSelector() class.

  • Launch the App on an Android device.
  • Click on the button using index value by using UiSelector() class
  • Wait for 2 seconds
  • Close the App

ByIndexValue.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 Index value"
ele_index = driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,"UiSelector().index(4)")
ele_index.click()

# Step 4 : Wait for 2 seconds
time.sleep(2)

# Step 5 : Close the driver object
driver.quit()