The next thing to do is to interact with the different elements of the application and for that it is required to locate elements first before interacting. Locating elements can be done on the AndroidDriverinstance itself, as it gives us ‘Find Element‘ and ‘Find Elements‘ method to locate elements on the Appium. In this chapter we will learn to use FindElement and FindElements commands or how to locate elements with Appium UiAutomatorViewer/ Appium Inspector with different attributes.

How to Use FindElement & FindElements Method in Appium

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, it can return an empty list if no DOM elements match the query. The Find methods take a locator or query object called By. ‘By’ strategies are listed below.

By Name

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 NoSuchElementExceptionwill be raised.

Example 1: If an element is given like this:

The above element can be located with the below commands:

1

2

3

driver.findElement(By.name("Shop byDepartment")).click();

//or

driver.findElementByName("Shop byDepartment").click();

Example 2: If an element is given like this:The above element can be located with the below commands:

1

2

WebElement element=driver.findElement(By.name("Home"));

element.click();

How to find element using ‘content-desc‘ attribute in Appium?

content-desc attribute can be used exactly like textattribute with findElementByName. Look at the example below:

Example 3: If an element is given like this:

The above element can be located with the below commands:

1

driver.findElementByName("Sign inHello. Link").click();

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.

Example: If an element is given like this:The above element can be located with the below commands:

1

2

3

4

driver.findElementByClassName("android.widget.ImageView").click();

//or

driver.findElement(By.className("android.widget.ImageView")).click();

Note: This will only work efficiently when this is a unique class name, which is generally not a case in Appium. But still we can use the className in conjunction with other, we will have a detailed example below for it.

By ID

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 NoSuchElementExceptionwill be raised. This is the most efficient and preferred way to locate an element, as most of the times IDs are unique.

By XPath

Xpathis a true blessing 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.

Let’s look at the example below for Password field of Amazon Sign In module. Password text box does not have any ID attached to it, not any text value and content-desc. But in this case we have className populated but again that is not help, as the className of the UserName text box is also same.

Example: If an element is given like this

If you inspect ‘(5) View{Password} […][…]‘ you will notice that the element got content-desc value as ‘Password‘. It means we can easily locate the (5)element with the attribute content-desc and then traverse down to (6)element and with that we can easily reach out to its child element EditText.

How to use Siblings in Xpath

But our scenario says that we need to locate child of the (6)element. Hence first it is required to locate (6) which is sibling of (5). In Xpath, there is a way to locate siblings elements. Below statement will locate the (6)element:

How to find Element using Parent node?

This is again a very important technique to locate element in Appium. There will be many situations where the only option to locate element is to get the parent element first and then get its child object to perform any action on child element.

Consider Username text box for this scenario. UserName text box does not have any ID attached to it, not any text value and content-desc. But in this case we have className populated but again that is not help, as the className of the Password text box is also same as ‘android.widget.EditText‘. If we go one step up to the parent of EditText, it is View with the className as ‘android.view.View‘ which is again not unique. So, we need to go one more step up for finding unique parent, which is Amazon Sign In shown in the below image:

With using above statement, we would be able to get the parent element, but we are interested in the child elements of the parent. With the below statement, we will get all the child elements ‘Views‘ which is under selected parent:

Now we have all the Views element with us in childElements, but we are interested in the 5th child element, as username text box is under that 5th View element. See the below screenshot for help.The above element can be located with the below commands:

Java

1

2

//This is to get the 5th child element

WebElement mainElement=childElements.get(4);

Note: We have mention .get(4) for 5th element because the index starts from zero.

Once we get the main element(5th), it is easy to point it’s child element: