Introduction to Locators

What is a Locator?

Identifying or locating the elements on the web or mobile using its value or property is known as locator.

We can find the element using below methods and locator types such as id , className , text , content-description etc.

WebDriver class of the appium client has two types of methods such as find_element() and find_elements() that helps us to identify the elements on App and perform the required operation on it.

We can access the methods find_element() and find_elements() by using the AndroidDriver object.

We can identify the elements by using locator types such as id, className , text, index, content-description and Xpath.

find_element()

This method is used to identify the mobile element by using those 6 types of locators.It takes locator type and locator value as arguments.

Syntax : driver.find_element(AppiumBy.Locator_Type,"Locator_Value")

This syntax gives an output as an MobileElement object and by using that object we can perform actions such as Click,sendtext, gettext, get content-description etc.

Syntax :

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

Here ele_id is a variable name.

find_elements()

This method is used to identify group of MobileElements which are assigned to any of those locator types.

Syntax : driver.find_elements(AppiumBy.Locator_Type,"Locator_Value")

This syntax gives an output as a list object and by using for loop we can iterate the list object and perform the required operation.

We will discuss more about those methods in later chapters.

find_element(AppiumBy.ANDROID_UIAUTOMATOR,"UiSelector()")

Android UIAutomator method is used to help identify the MobileElement using the android inbuilt method UiSelector() by "id, classname , text, content-description , index".

This method is applicable to find elements only on android devices.It doesn’t work on other OS devices.

It takes String as an argument. If an element isn't identified it throws an exception as No Such Element Exception.

Syntax :

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

Below are the locator types to find element using find_element() and find_elements()

  • id
  • className
  • text
  • By Index number
  • By Content-description
  • xpath

There are two types of XPaths

  • Relative Xpath
  • Absolute Xpath