Appium Android Test Using Different Element Locators

Earlier in THIS POST, We learnt different element locators of android app like XPath, ID, Name And className to use in appium automation test script. We also learnt android application's element locating strategy using findElements method of webdriver. I have prepared very simple appium android app test of real android mobile device's calculator application where I have used all above element locating strategies so that you can understand android app element locators better.

PREREQUISITES : Appium android tutorial articles 1 TO 15 and 16 TO 17 should be completed.

After reading all previous appium tutorials, I hope you have enough knowledge on how to run appium test on real android device or emulator. Now let's Implement appium android test and understand how to use different element locators in android appium automation test. Follow steps given bellow.

1. Connect your android device with PC and start USB debugging mode. View THIS POST.
2. Launch and start appium server. View THIS POST.
3. Install TestNG in eclipse if it is not installed. View THIS POST.
4. Create bellow given appium android test script under Android package of your project in eclipse.
package Android;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class ElementLocatorTest {

 WebDriver driver;

 public void setUp() throws MalformedURLException {
  // Created object of DesiredCapabilities class.
  DesiredCapabilities capabilities = new DesiredCapabilities();

  // Set android deviceName desired capability. Set your device name.
  capabilities.setCapability("deviceName", "ZX1B32FFXF");

  // Set BROWSER_NAME desired capability. It's Android in our case here.
  capabilities.setCapability("browserName", "Android");

  // Set android VERSION desired capability. Set your mobile device's OS version.
  capabilities.setCapability("platformVersion", "4.4.2");

  // Set android platformName desired capability. It's Android in our case here.
  capabilities.setCapability("platformName", "Android");

  // Set android appPackage desired capability. It is for calculator application.
  // Set your application's appPackage if you are using any other app.
  capabilities.setCapability("appPackage", "");

  // Set android appActivity desired capability. It is for calculator application.
  // Set your application's appPackage if you are using any other app.
  capabilities.setCapability("appActivity", "");

  // Created object of RemoteWebDriver will all set capabilities.
  // Set appium server address and port number in URL string.
  // It will launch calculator app in android device.
  driver = new RemoteWebDriver(new URL(""), capabilities);
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

 public void Sum() {
  // Using findElements.
  // Locate DELETE/CLR button using findElements and get() method and click on it.

  // By xpath.
  // Locate number button 2 by XPATH element locator and click on it.

  // Using findElements.
  // Locate number button + using findElements and get() method and click on it.

  // By id.
  // Locate number button 5 by ID element locator and click on it.

  // By name.
  // Locate number button = by name element locator and click on it.

  // By className.
  // Locate result textbox by CLASSNAME element locator and get result from it.
  String result = driver.findElement(By.className("android.widget.EditText")).getText();
  System.out.println("Number sum result is : " + result);


 public void End() {

Each and every element locator is already explained in comment with test script's each statement. You can see in above script, I have used all different element locators to locate different elements of android calculator app.
  • findElements with get() method : findElements with get() method element locating strategy is used to locate DELETE and + buttons of calc app. Here "android.widget.Button" is common class name of all buttons. So It will find all elements which contains class name = "android.widget.Button" and then get() method will locate given Indexed element from list. DELETE button has index = 0 and + button has index = 16.
  • xpath : xpath element locator is used to locate number button 2. Here "android.widget.Button" is class name and then it will look for element where text value = "2".
  • id : id element locator is used to locate number button 5. Here it will directly look for element where resource-id="".
  • name : name element locator is used to locate = button. In this case, It will look for element where text value = "=".
  • className : className element locator is used to locate result text area. It will find element where class value = "android.widget.EditText".
I hope, this is enough to understand android app element locators. You can use all above described element locators for any application.

Now you can run above appium android test script using testng(Right click on test case -> Run As -> TestNG Test) in eclipse. It will print result two number's sum result in console.


  1. for the element with text, we canot find that element
    Is that really working for you???

  2. Hi,

    I am running the code in emulator. I first started Emulator, then Appium server and then ran the code using testNG but I am getting error "org.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: Could not find a connected Android device.) (WARNING: The server did not provide any stacktrace information)."
    My emulator version is 4.4.2

    I am getting same error when running in real Android device.Till now I am not able to run code successfully either in emulator or in real device due to mentioned error. Can you please help.

    1. Use samsung galaxy s5 emulator (4.4.4) API 19 and before run make sure ADB recognizes device.

  3. here you are using remotedriver, but in next chapter you start using androiddriver, is there any reason that you use different drivers here?