Tuesday 1 June 2021

Python Appium - Locating elements using xpath,class,accessibility ID,ID and Image

 Appium Locators

There are five locators which are extensively used for identifying mobile element using appium inspector. So let us see each with examples.

You can also check

ID

This can be used when you know the id attribute of an element. If the element is not found by mentioned attribute, it would raise an exception "NoSuchElementException"

So let us take example of dialer page. Here we have launched dialer screen using below desired capabilities
desired_caps = {
'platformName': 'android',
'udid': 'emulator-5554',
'deviceName': 'Pixel 1 API 28',
'platformVersion': '9.0',
'appPackage': 'com.google.android.dialer',
'appActivity': 'com.google.android.dialer.extensions.GoogleDialtactsActivity'

}
In above yellow marked item was identified by any of the locators so I have decided to go by co ordinate method. Third option given appium inspector is 'Tap By Co-ordinates'. For co ordinates, first click on recording and click on the dialpad item. you would see auto generated code for it as below
You can include above code in your automation script as below
from appium.webdriver.common.touch_action import TouchAction
'''Tap on dialer pad, as we could not see any valid element identifiers'''
touch = TouchAction(driver)
touch.tap(x=955, y=1515).perform()
So I have selected python language here, you can select any other language in which you are writing your automation scripts. Copy paste python code in your script. On this page let us click on dial for id attribute
Let us check whether we have found a unique identifier. On appium inspector there is an option on top beside recording option called 'Search for Element'.


So once it is validated here, you can include it in your code as below
driver.find_element_by_id("com.google.android.dialer:id/digits")
You need to type some number on the element you have identified for this you can use send_keys() method.
driver.find_element_by_id("com.google.android.dialer:id/digits").send_keys("810000000")
Complete code would look like as below
import pytest
from appium import webdriver
from time import sleep
from appium.webdriver.common.touch_action import TouchAction

def __init__(self, driver):
self.driver = driver

def test_locators():
desired_caps = {
'platformName': 'android',
'udid': 'emulator-5554',
'deviceName': 'Pixel 1 API 28',
'platformVersion': '9.0',
'appPackage': 'com.google.android.dialer',
'appActivity': 'com.google.android.dialer.extensions.GoogleDialtactsActivity'

}
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)

'''Locate Element by ID'''
'''Tap on dialer pad, as we could not see any valid element identifiers'''
touch = TouchAction(driver)
touch.tap(x=955, y=1515).perform()
driver.find_element_by_id("com.google.android.dialer:id/digits").send_keys("810000000")
print("\n Element by ID is found")
To execute run below command,
py.test test_locators.py -v -s
Classname
This can be used when you know the classname attribute of an element. If the element is not found by mentioned attribute, it would raise an exception "NoSuchElementException"

Let us take example of calculator native app. The desired capabilities would be like as below
desired_caps = {
'platformName': 'android',
'udid': 'emulator-5554',
'deviceName': 'Pixel 1 API 28',
'platformVersion': '9.0',
'appPackage': 'com.android.calculator2',
'appActivity': 'com.android.calculator2.Calculator'

}
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
Code snippet for identify element by class name would be as below
'''Locate Element by Class'''
driver.find_element_by_class_name("android.widget.ImageButton")
print("Class Element is found")


Accessibility ID:
This can be used when you know the accessibility id attribute of an element. If the element is not found by mentioned attribute, it would raise an exception "NoSuchElementException"

Let us take example of flipkart app for this.

The code would look like as below,
desired_caps = {
'platformName': 'android',
'udid': 'emulator-5554',
'deviceName': 'Pixel 1 API 28',
'platformVersion': '9.0',
'noReset': 'true',
'appPackage': 'com.flipkart.android',
'appActivity': 'com.flipkart.android.activity.HomeFragmentHolderActivity'

}
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
time.sleep(20)
'''Locate Element by Accessibility ID'''
driver.find_element_by_accessibility_id("Flipkart home")
print("Element found")
Xpath
This can be used when you know the xpath attribute of an element. If the element is not found by mentioned attribute, it would raise an exception "NoSuchElementException"

The code would look like as below,
desired_caps = {
'platformName': 'android',
'udid': 'emulator-5554',
'deviceName': 'Pixel 1 API 28',
'platformVersion': '9.0',
'noReset': 'true',
'appPackage': 'com.flipkart.android',
'appActivity': 'com.flipkart.android.activity.HomeFragmentHolderActivity'

}
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
time.sleep(20)
'''Locate Element by Xpath'''
driver.find_element_by_xpath("//android.widget.ImageView[@content-desc='Flipkart home']")
print("Element found by xpath")

Image:
This can be used when you know the image attribute of an element. If the element is not found by mentioned attribute, it would raise an exception "NoSuchElementException"
desired_caps = {
'platformName': 'android',
'udid': 'emulator-5554',
'deviceName': 'Pixel 1 API 28',
'platformVersion': '9.0',
'noReset': 'true',
'appPackage': 'com.flipkart.android',
'appActivity': 'com.flipkart.android.activity.HomeFragmentHolderActivity'

}
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
time.sleep(20)
'''Locate Element by Image'''
driver.find_element_by_image('c://sample/2021/to/img.png')
print("Element found by Image")


No comments:

Post a Comment

Feature Posts

Python Appium - Step by step procedure for capturing screenshot

 Why To capture screenshot? It is as important as your logs. If there is any failure for any test scenario, we can provide screenshot for th...