Identify elements in Android and iOS

Hi All,

I need to identify the elements/objects of an app. For now I am using Android.

Following code works fine.

I need help in locating any elements with their id or some other way. I don’t want to use elements absolute path as ‘//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[4]/android.widget.Button[3]’

#!/Library/Frameworks/Python.framework/Versions/2.7/bin/python

from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.common.action_chains import ActionChains
import time
import os
from appium import webdriver

success = True
desired_caps = {}
desired_caps['appium-version'] = '1.0'
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '4.4'
desired_caps['deviceName'] = '2a22693'
desired_caps['app'] = os.path.abspath('/Jagadeesh/Testing/Automation/Appium/TestScripts/AUT/Test.apk')
#desired_caps['appPackage'] = 'org.mozilla.firefox'
#desired_caps['appActivity'] = 'App'

wd = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
wd.implicitly_wait(60)

def is_alert_present(wd):
	try:
		wd.switch_to_alert().text
		return True
	except:
		return False

def valid_login():
	#Tap on '9' button
	print '**********************************'
	print 'Set new Passcode.'
	print '**********************************'
	wd.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[4]/android.widget.Button[3]").click()
	print 'Taped on \'9\' button.'
	#Tap on '8' button
	wd.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[4]/android.widget.Button[2]").click()
	print 'Taped on \'8\' button.'
	#Tap on '8' button
	wd.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[4]/android.widget.Button[2]").click()
	print 'Again Taped on \'8\' button.'
	#Tap on '0' button
	wd.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[5]/android.widget.LinearLayout[1]/android.widget.Button[1]").click()
	print 'Tapped on \'0\' button'
	print '**********************************'
	print 'Passcode set completed.'
	print '**********************************'
	print '\n'
	print '**********************************'
	print '\nLogging in using Passcode.'
	print '**********************************'
	
	#Again Tap on '9' button
	wd.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[4]/android.widget.Button[3]").click()
	print 'Taped on \'9\' button.'
	#Tap on '8' button
	wd.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[4]/android.widget.Button[2]").click()
	print 'Taped on \'8\' button.'
	#Tap on '8' button
	wd.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[4]/android.widget.Button[2]").click()
	print 'Again Taped on \'8\' button.'
	#Tap on '0' button
	wd.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[5]/android.widget.LinearLayout[1]/android.widget.Button[1]").click()
	print 'Tapped on \'0\' button'
	print '**********************************'
	print 'Loggedin successfully.'
	print '**********************************'

def valid_logout():
	print '**********************************'
	#Tap on 'Logout' button
	wd.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.widget.ImageView[1]").click()
	print 'Tapped on \'Logout\' button'

	# Tap on 'Yes' for Confirmation to Logout
	wd.find_element_by_xpath("//android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[3]/android.widget.LinearLayout[1]/android.widget.Button[1]").click()
	print 'Tapped on \'Yes\' button for Confirmation to Logout'
	print '**********************************'

try:
	valid_login()	
	
	# Add 4 Notes with Title and Message
	for i in range(1,4):
		# Tap on 'Add' button
		wd.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[2]/android.widget.ImageView[1]").click()	
		# Tap on Title field and Enter Title
		wd.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.ScrollView[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.EditText[1]").send_keys("TestTitle_",i)
		# Tap on Message field and Enter Message
		wd.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.ScrollView[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.EditText[2]").send_keys("Message..........................Message.................................Message..................................Message........Message ")
		# Tap on 'Add' button
		wd.find_element_by_xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.ScrollView[1]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[1]/android.widget.Button[1]").click()
		print 'TestTitle_',i,' Note successfully created.'

	valid_logout()
	
	
finally:
	wd.quit()
	if not success:
		raise Exception("Test failed.")

Regards,
Jagadeesh
Email: [email protected]

find_elements_by_accessibility_id
find_element_by_name
find_element_by_class_name

I dont use python, but just explore the client: https://github.com/appium/python-client

Thanks for the replay Telmo

Probably this link can give you more idea with respect to locator strategy. Vote-up if you find it useful. Do check the sample code & additional info @ github