Thursday, 11 February 2016

How to use APIs to Locate elements and perform actions?

This is basic Appium tutorial for all who want to start with Appium Automation. I will give you brief description of how to use Appium APIs for selecting particular Mobile element and performing desired operation on it.

Good for all of us Appium provides a list of user-friendly APIs for locating elements using ids, xpath, class name, content description etc. Once we locate the element next thing we want to do is perform operations like type text, click a button, long press, swipe, scroll etc.

In this post I will show you most frequently used APIs while writing test cases with example.This basics actions and assertions will help you to quickly start writing test cases. Have a look at these sample test case and comment if you have any doubts.

Video -

How to Use Find Element and FindElements methods in Appium - (FindElement Vs FindElements)

The difference between Find Element and Find Elements method is the first returns a WebElement object otherwise it throws an exception and the latter returns a List of WebElements,  The Find methods take a locator or query object called By. ‘
let us learn about By’ strategies  -

By Name (By Content-desc)

This is also an efficient way to locate an element but again the problem is same as with ID that UI developer make it having non-unique names on a page or auto-generating the names. With this strategy, the first element with the name attribute value matching the location will be returned. If no element has a matching name attribute, a NoSuchElementException will be raised.


By Class Name

With this you can find elements based on the value of the class attribute. If an element has many classes then this will match against each of them. A class can contain many elements.


With this strategy, the first element with the id attribute value matching the location will be returned. If no element has a matching id attribute, a NoSuchElementException will be raised. This is the most efficient and preferred way to locate an element, as most of the times IDs are unique.

By XPath

Xpath is important locator in Appium, as you will end up many situation where you do not have any options but to use xpath. Xpath can be relative and absolute but it is suggested to always use relative xpath.

Sample Test Case Example -


 public void testEditText()
    //Find element by id and enter Hello in Text box.

    //Find element by class name and enter Hello in Text box.

    //Find element by xpath and enter Hello in Text box.

    driver.findElementByXPath("//cass[@value='Enter Name']").sendKeys("Hello");
    //Find element by link text and enter Hello in Text box.

    driver.findElementByLinkText("Enter Name").sendKeys("Hello");
    Assert.assertEquals("Hello", driver.findElementByLinkText("Enter Name").getText());


@Testpublic void testButtonClick()

@Testpublic void testCheckBoxSelection()

@Afterpublic void testCaseTearDown()

I hope from the above samples it is clear to you how to use different properties like class name, resource id , xpath etc to select a element and to perform type & click actions which are most commonly used.

Once you are done with this tutorial then you can start writing your test cases right away because now you are clear with how to locate an element and perform action on it so you can start with converting your manual test cases into automated scripts.

If you find this tutorial helpful please share it with your friends and leave your questions or suggestions in comment box . Give your feed back guys it helps me a lot to write more and more. Thanks for reading and sharing


  1. Hi this is maheswaran, Having 4+ years of exp in Android native applicaiton development. Right now i am following your Appium tutorials (simply u did grate job). I have done all initial setup. I struggling to write test case, so kindly can you share me the source code for login page and navigate to home screen.

    1. Hi, Thanks for your comments. You can refer the above test case and also view the post on how to locate elements using ids and xpath once you learn about this then you can perform actions like type and click and you can automate login screen in same way.

      Thanks & Regards,