Home » appium-tutorial » FindElement & FindElements Command in Appium

Newly Updated Posts

FindElement & FindElements Command in Appium

Previou Topic, In this tutorial we will learn how to FindElement & FindElements Command in Appium.

If we wish to interact the elements with in mobile device, the first step is to interact with elements, Android driver instance provides the method “FindElement” and “FindElements” to locate the elements in mobile application.

How to implement FindElement And FindElements Command with Appium

The major difference between FindElement And FindElements is that FindElement returns a WebElement and FindElement returns a list of WebElements.

The different types of locators are below:

1 ) By ID: By using this ID attribute the Appium inspector find the first element with the same id value and return the object. This is the most adopted way of locating the element, as most of the time the ID is unique. If the locator is not unique it displays an error message as “NoSuchElementException”.

Syntex for ID Attribute is below:

//driver.findElementById("com.flipkart.android:id/search_widget_textbox");
OR
//driver.findElement(By.id("com.flipkart.android:id/search_widget_textbox");
FindElement & FindElements Command in Appium

2 )By Name: The other way of locating the element is by Name of the element. We define the text of the locator and it return the object of the element but problem arises is that the text is not always unique.

Syntex are below:
//driver.findElementByName("Search for Products, Brands and More");
OR
//driver.findElement(By.name("Search for Products, Brands and More"));
FindElement & FindElements Command in Appium

How to locate Element by Content-desc?

Note : If the attribute type is “content-desc” it will be handled in the same way as we handle the attribute text, by By name method.

Handling attribute “content-desc” are as below:

Syntex of "content-desc" are below:
// 
//driver.findElementByName("Open Drawer");
OR
//driver.findElement(By.name("Open Drawer"));

3 ) By Class: We can also locate the element using class attribute but the problem again arises regarding uniqueness, As there can be multiple elements be found with same class name.

Syntex of "ClassName" are below:

//driver.findElementByClassName("android.widget.ImageView");
OR				/driver.findElement(By.className("android.widget.ImageView"));
FindElement & FindElements Command in Appium

4) By AccessibilityId: You can also find element by accessibilityID, the accessibility id provided by appium inspector for the element in mobile application can be given by below syntex.

Syntex"
//driver.findElementByAccessibilityId("Shop");

5) By Xpath: This method is the most preferred way of object identification in all area for, WebApplication along with mobile application.As we come up with many elements where unique elements or the id is not available.

So to handle such elements we define locator or identify locator by xpath.

To understand the concept of xpath, consider a scenario, where we are identifying the locator , by different attribute name.

FindElement & FindElements Command in Appium

So to find the locator we will use by class : android.widget.ImageButton

but the class is not unique and we do not find other attribute that is unique within this page. So the solution for such elements is using xpath.

We generally created Xpath by the combination of different attribute and also by traversing from parent to child element. Here will locate xpath by the combination of different attibute value shown below.

//Syntex
driver.findElement(By.xpath("//android.widget.ImageButton[@content-desc="Open Drawer"]"));
OR

//driver.findElementByXPath("//android.widget.ImageButton[@content-desc="Open Drawer"]");

So here we made combination of class attribute and content-desc attribute to create a xpath as: //android.widget.ImageButton[@content-desc=”Open Drawer”]

locate elements using xpath in appium