Finding elements using locators in Appium

As shown in the previous article, we can identify elements of Native & Hybrid apps using UIAutomatorviewer. There are few locators used to locate elements for native and hybird apps. Okay let’s look into it. Before that launch your emulator or connect real device. If you don’t know how, check my article. We’ll locate elements of Calculator app. Now Launch UIAutomatorviewer and capture the screenshot of calculator app.


Now, let’s see different locators supported by the Appium driver.

  • Finding elements by ID
  • Finding elements by name
  • Finding elements by className
  • Finding elements by AccessibilityId
  • Finding elements by AndroidUIAutomator
  • Finding elements by Xpath

All the syntax was same as in Selenium. No need to worry. We will see each and every locator here.

1. Finding elements by ID (resource-id)
Using this ID we can locate an element. Let’s try to find the digit 5 from the calculator app using UI Automator. Just Click on number ‘5’ in UIAutomator.

Node details of that element will be displayed. In those details get the value of ‘resource-id’. And pass the value in the syntax.
Now coming to syntax: driver.findElement(“resource-id Value”));
As of now for digit ‘5’, driver.findElement(“”)).click();

2. Finding elements by name (text)
For this the syntax would be findElement( of element));
Lets take ‘+’ sign. To click on that button, we need a locator. So click on ‘+’ button in UIAutomator. In node details you will ‘text’ having value ‘+’.


The syntax would be: driver.findElement(“+”)).click();

3. Finding elements by className (class)
We can locate an element using the className as well. This is how the syntax looks like driver.findElement(By.className(class value));
In the below image, you can see that class name was ‘android.widget.Button’. So the syntax would be:


Do you think it works? No! Because all the buttons has the same class name. Now what we suppose to do. In order to click on ‘+’ button using class name, we need to store all the elements having that className and store it in a list. The syntax would be like:
Here, the ‘+’ button has an index value 16, so we have to pass an index value of 16 to take perform action(Just count the buttons in rows)

4. Finding elements by AccessibilityId
Accessibility ID usually have text associated with element. As per Appium Docs, accessibility id: a string corresponding to a recursive element search using the Id/Name that the native Accessibility options utilize. This is how the syntax for AccessibilityId looks:
driver.findElement(By.AccessibilityId(AccessibilityId value));

As of now, let’s check with ‘+’ button again. Click on the + sign from UIAutomator. Now under node Details, you will get the ‘content-desc’ as plus. Take a look at below image


Now the syntax would be like: driver. findElementByAccessibilityId(“plus”);

5. Finding elements by AndroidUIAutomator & UISelector
AndroidUIAutomator is a very powerful locator to find an element. It uses UIselector to find an element. Syntax looks like: driver.findElementByAndroidUIAutomator(“String”);
If you would like to get a tag name of an element then
WebElement element = driver.findElementByAndroidUIAutomator(“new UiSelector().index(0)”);
assertEquals(“android.widget.FrameLayout”, element.getTagName());

Here I have passed UISelector as an argument. UiSelector has many more methods in-built. You can verify whether element is scrollable, clickable etc..

To pick ‘content-desc‘ as equals,then syntax looks like
WebElement test=driver. findElementBy.AndroidUIAutomator(“new

6. Finding elements by Xpath
Just like in Selenium, we can use Xpath to locate an element. We can also customize xpath using axis. Though xpath works bit slower compared to ID, this helps us in locating exact element. Syntax would be like:
driver.findElement(By.xpath(xpath of element));

I know this is simple syntax. Now, let’s see few customized xpaths to locate an element

a. XPath using class name and ‘text’

XpathWithTextIn above ‘android.widget.Button’ is a classname and text value is ‘5’

b. Xpath using ‘contains’

Xpath_ResourceID_TextIn above command we are using contains to locate an element having ‘android.widget.Button’ class name and having ‘resource-id’ = ‘digit5’

c. Xpath using ‘contains’ and ‘and’ to club locator values
driver.findElement(By.xpath(“//android.widget.Button[contains(@resource-id,’digit5′) and @text=’5′]”));
In above command, we are locating an element having class name ‘android.widget.Button’ which contains ID ‘resource-id’ and having ‘text’

We can customize xpath in many ways. Here I’m not posting all about it. You can find few basics of it here. Also you can surf in google to know more about xpath customization. Now you’re good with basic part of Appium. Let’s dive into coding part.

10 thoughts on “Finding elements using locators in Appium

  1. bhaskar August 24, 2016 / 8:19 pm



  2. Siddhesh Sahane February 6, 2017 / 10:44 am

    Hi, I would like to know how to create an xpath with smart way like using parent and sibling for android element…
    Could you please help?


  3. Ketam Srinivas July 31, 2018 / 12:28 pm


    For this, It is asking List instead of List in the Eclipse


    • Hari Charan August 20, 2018 / 12:02 am

      Hello Ketam, could you please share the exception you are facing.


  4. Ketam Srinivas July 31, 2018 / 1:24 pm

    in java-client version: ‘5.0.4’ – driver.findElement(By.AccessibilityId(AccessibilityId value)); -> Is this working, For us it not working.


    • Hari Charan August 20, 2018 / 12:02 am

      I am not certain. Since the API has got a lot of changes till date, this post seems outdated and require a modification. Will look at this again in free time.


  5. Jaga Nani October 29, 2018 / 2:33 pm

    How to automate images in appium??


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s