Interacting with Elements

Everything what you see in an app is a visual representation of native element. These elements refers to text boxes, checkboxes, buttons, radio buttons, spinners and many more that display or accept data from the user. These elements are designed using native code. When we interact with these elements, we are interacting with these native codes.

In this chapter we will learn how to interact with different native elements.

The most commonly used native elements are as follows:-

  • Textbox
  • Button
  • Labels
  • Radio buttons
  • Checkboxes
  • Spinners (Dropdown)
  • Lists

To interact with these elements we need to identify it first. This is done with the help of locators. You need to find the right and unique locator for the element and use the web related methods to interact with these elements.

There are two methods provided by appium to find the element.

  • Find Single Element
  • Using findElement() method of Appium, unique single element can be located. Here the expectation is that the locator is unique and will return a single element.


  • Find List of Elements
  • Using the findElements() method of Appium, more than one element matching the locator can be retrieved. This is when the targeted element is part of a list or with common locator and can't be identified uniquely.

Click

Click method is used to click/tap on an element. It can be used on any element which accepts click/tap event, but the most ideal use case are on the following elements:-

  • Button
  • Radio button
  • Checkbox
  • Spinner item
  • List item

This method will click the element on its in-view center point. If the element's center point is obscured by another element, an element click intercepted error is returned. If the element is outside the viewport, an element not interactable error is returned.

Java
C#
Python
Javascript
Kotlin
Copy
WebElement loginButton = driver.findElement(By.id("loginButton"));
loginButton.click();

Send Keys

The sendKeys method types the provided Char Sequence into an editable element. Typically, this means an element is an input element or some sort of textbox which accepts user inputs. If it is not editable, an invalid element state exception is thrown.

Java
C#
Python
Javascript
Kotlin
Copy
driver.findElement(By.id("username")).sendKeys("admin");
driver.findElement(By.id("password")).sendKeys("admin");
driver.findElement(By.id("loginButton")).click();

Clear

The clear method resets the content of an element. This requires an element to be editable. The element should be of a textbox or editable text.

Java
C#
Python
Javascript
Kotlin
Copy
WebElement userNameElement = driver.findElement(AppiumBy.id("username"));
userNameElement.clear();

getAttribute

The getAttribute method returns the value of an element's attribute as a string. If there is no attribute, this method will return null. This method is commonly used to get attribute value from an element.

Java
C#
Python
Javascript
Kotlin
Copy
WebElement userNameElement = driver.findElement(AppiumBy.id("username"));
String attributeValue = userNameElement.getAttribute("hint");
System.out.println(attributeValue);

getText

This method will return the text of the given element, including the sub-elements.

Java
C#
Python
Javascript
Kotlin
Copy
WebElement userNameElement = driver.findElement(AppiumBy.id("username"));
String textValue = userNameElement.getText();
System.out.println(textValue);

isDisplayed

This method will check if the element is displayed or not. It will return true if the element is displayed else will return false. This operation will work on all elements.

Java
C#
Python
Javascript
Kotlin
Copy
WebElement userNameElement = driver.findElement(AppiumBy.id("username"));
Boolean isDisplayed = userNameElement.isDisplayed();
System.out.println(isDisplayed);

isEnabled

This method will check if the element is enabled. It will return false if the element is disabled else will return true. This operation will work on all elements.

Java
C#
Python
Javascript
Kotlin
Copy
WebElement userNameElement = driver.findElement(AppiumBy.id("username"));
Boolean isEnabled = userNameElement.isEnabled();
System.out.println(isEnabled);