Appium- Article #3

🎯Ways To Target an Element

Select an Element on the page📃

Fareena Imran
CodixLab

--

In this article, we’re going to learn about how to target an element on the page in different ways. Let’s get started 👇

Here are some useful ways that are used to target an element on the page.

If you don't know how to inspect an element of App then you can read my previous article (Appium- Article #2)

By ID

You can use the id of the target element to select it on the page. For Example:

findElementById("android:id/checkbox");

By Xpath

You can define an Xpath for the target element as I explained in the previous lecture. For Example:

findElementByXPath("//android.widget.TextView[@text='Preference']");

By Class Name

The class name can also be used when there is no unique attribute defined for the target element. For Example:

findElementByClassName(“android.widget.EditText”);

By Elements

All the above ways are targeting the specific element by findElement but when we want to get all the elements on the page that have the same class, id, etc. Then we use the keyword findElements. For Example:

findElementsByClassName(“android.widget.EditText”).get(3);

The.get() will hit the specific element with a defined index like there maybe 4 or 5 elements on the page with the same class name (android.widget.EditText) but you need to target the 3rd one then you can use this way to hit that element.

By AndroidUIAutomator for Attribute

You can use the below syntax to select an element by AndroidUIAutomator

findElementByAndroidUIAutomator("attribute(\"value\")");

By AndroidUIAutomator for Property

You can use the below syntax to select an element by AndroidUIAutomator

findElementByAndroidUIAutomator("new UiSelector().property(value)");

Follow my articles, to learn more.

If you find this post useful, please tap 👏 button below 😊

Best of luck 👍

--

--

Fareena Imran
CodixLab

SQA Engineer| Tech Writer | Write stories about SQA