find_elements()

This method is used to identify the Elements using all 6 types of locators.But it gives a list object , We need to iterate using for loop to do required operation on it.

Syntax :

driver.find_elements(AppiumBy.Locator_Type,"Locator_Value")

Example: In the below example we are launching the app on an Android device and clicking on the “ScrollView” button.

  • Launch the App on an Android device.
  • Using class name locator value gets all the elements into list value.
  • Using for loop, iterate the list value and click on the "ScrollView" button.
  • Wait for 2 seconds
  • Close the App

FindElementsMethods.py

from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
import time

# Step 1 : Create "Desired Capabilities"
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['automationName'] = 'UiAutomator2'
desired_caps['platformVersion'] = '10'
desired_caps['deviceName'] = 'Pixel3XL'
desired_caps['app'] = ('/Skill2Lead/Appium_Demo_App/Android/Android_Appium_Demo.apk')
desired_caps['appPackage'] = 'com.skill2lead.appiumdemo'
desired_caps['appActivity'] = 'com.skill2lead.appiumdemo.MainActivity'

# Step 2 : Create "Driver object"
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)

# Step 3 : "Using class name locator value gets all the elements into list value"
element = driver.find_element(AppiumBy.CLASS_NAME,"android.widget.Button")

# Step 4 : Using for loop, iterate the list value and click on the "ScrollView" button.
for x in element:
    button = x.text
    if button == "ScrollView":
        x.click()
        break

# Step 5 : Wait for 2 seconds
time.sleep(2)

# Step 6 : Close the driver object
driver.quit()

Code Explanation

1. Here we are using class name locator and storing all the values in a list object

element = driver.find_element(AppiumBy.CLASS_NAME,"android.widget.Button")

Now using for loop, iterate all the values in the list object and get the text from the list object using “text” property.

x.text

3. Now check for the value for which we need to perform our action on it using the “if” condition.

If the condition is satisfied then break the statement.

Here we are clicking on the “ScrollView” button.

if button == "ScrollView":
   x.click()
   break