appium元素定位
元素定位方法
1)id定位: resource-id属性值
2)class定位: class属性值
3)xpath定位: xpath表达式
driver.find_elements_by_xpath("//*[contains(@text,'设')]")
driver.find_element(By.XPATH,'//android.widget.EditText').send_keys('你好')
4)content定位:content-desc属性值
driver.find_element_by_accessibility_id(content-desc属性值)
定位一组元素elements
返回的是一个例表,可以通过下标来调用列表中的元素,
如果定位不到元素,不会报错,返回空列表
元素等待跟web一样
隐式等待driver.implicitly_wait(timeout)
显示等待WebDriverWait(driver, 10).until(lambda x:x.find_element(By.ID, 'value'))
强制等待time.sleep(3)
以下知识点实例汇总:
import time
from appium import webdriver
# 启动参数
from selenium.webdriver.common.by import By
############cap参数#######################################
cap = {
"platformName": "Android",# 被测试手机设备系统型号
"deviceName": "xxx",# 被测试手机的设备号。如果只连了一个设备,
# 可以用 **** 代替,如果连了多个设备,一定要写对应的设备名称
"appPackage": "com.android.settings",# 被测试的APP的包名
"appActivity": ".Settings",# 被测试的APP的启动名
# (启动名属于界面名,也就是app打开的第一个界面)
"autoGrantPermissions":True, # 是否自动授权 是
"noReset": True # 是否不重置应用 是
}
# 创建驱动
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', cap)
#########background后台#######################################
# 需求:打开《设置》应用,进入后台 5 秒,再回到前台
driver.background_app(1)
#############wait等待############################################
driver.implicitly_wait(3)#隐式等待
##############elements定位元素打印其文字内容#################################
# ①.使用ID定位,获取所有 resource-id 为 ”android:id/title“ 的元素,并打印其文字内容
elx_list=driver.find_elements_by_id('android:id/title')
print(len(elx_list))#打印数量
print("%"*15)
# ②.使用CLASS定位,获取所有class为”android.widget.TextView“的元素,并打印其文字内容
el_list=driver.find_elements_by_class_name('android.widget.TextView')
print(len(el_list))
print("%"*15)
# ③.使用XPATH定位,获取所有包含 ”设“ 的元素,并打印其文字内容
e_list=driver.find_elements_by_xpath("//*[contains(@text,'设')]")
print(len(e_list))#打印包含数量
for e in e_list:#遍历打印文本
print(e.text)
print('#################get_attribute定位元素打印文本数据信息#############')
for e in e_list:#遍历打印文本数据信息
print(e.get_attribute('resourceId'))
print(e.get_attribute('text'))
print(e.get_attribute('name'))
print(e.get_attribute('className'))
print(e.get_attribute('enabled'))
print('*'*12)
##############定位元素location位置siz宽高#############################
# ①.使用ID定位,定位“放大镜”按钮,resource-id并点击
driver.find_element(By.ID,'com.android.settings:id/search').click()
# 定位“输入框”定位位置大小
box_el=driver.find_element(By.CLASS_NAME,'android.widget.EditText')
loc=box_el.location
size=box_el.size
print('搜索框位置:',loc)
print('搜索框大小:',size)
##############定位元素方式########################################
# ②.使用CLASS定位,定位“输入框”class输入“hello”
driver.find_element(By.CLASS_NAME,'android.widget.EditText').send_keys('hello')
time.sleep(1)
###########clear清空所有文本内容########################################
driver.find_element(By.CLASS_NAME,'android.widget.EditText').clear()
time.sleep(1)
# 使用XPATH定位输入”你好”
driver.find_element(By.XPATH,'//android.widget.EditText').send_keys('你好')
# ③.使用XPATH定位,//class定位“返回”,并点击
driver.find_element(By.XPATH,'//android.widget.ImageButton').click()
# ④.使用accessibility_id定位,content-desc定位“放大镜”按钮,并点击
driver.find_element_by_accessibility_id('搜索设置').click()
driver.find_element_by_accessibility_id('收起').click()
driver.implicitly_wait(3)#隐式等待
###############current获取界面名和包名############################
# 打开《设置》应用程序,等待三秒获取界面名和包名
print('当前的包名',driver.current_package)
print('当前的界面名',driver.current_activity)
##############close_app关闭当前app页面#####################
# 打开《设置》,使用 close_app() 方法关闭,再尝试使用 quit() 方法,
driver.close_app()#关闭当前app页面
# 执行操作
# 打开《设置》应用程序,等待三秒后跳转到《TPshop》应用程序
time.sleep(1)#在cmd中输入adb logcat|findstr Displayed日志查找包名和界面名
###################start_activity查找到的数据传值切换界面######################
driver.start_activity('com.tpshop.malls','.SPMainActivity')
time.sleep(1)
# 最后打印当前程序的包名,观察区别
print('当前的包名',driver.current_package)
print('当前的界面名',driver.current_activity)
driver.close_app()#关闭当前app页面
################install_app安装和remove_app卸载##########################
# 如果《学车不》已经安装,则卸载《学车不》,如果没有则安装
app=driver.is_app_installed('com.bjcsxq.chat.carfriend')
print('查看app是否安装:',app)
if app:#如果已安装就卸载(包名)
driver.remove_app('com.bjcsxq.chat.carfriend')
else:#否则(如果未安装)就安装绝对路径下的apk
driver.install_app('D:8.ruanjian/XueCheBu.apk')
time.sleep(2)
# 关闭驱动
driver.quit()


