在做UI自动化的时候,有一部分精力是定位元素,元素定位得准不准,直接影响自动化的成败和效率。 mobile和web一样,也是通过driver来定位元素的。selenium有8种定位方法,appium有哪些定位方法呢? 常用的方法有:
id
定位元素class_name
定位元素content-desc
定位元素text
定位元素xpath
定位元素uiautomator
定位元素,android独有一般的元素属性有:
driver.find_element_by_android_uiautomator(uia_string)
driver.find_element_by_accessibility_id()
driver.find_element_by_xpath()
id
定位元素find_element_by_id
# resource-id = 'io.manong.developerdaily:id/edt_phone'
driver.find_element_by_id("io.manong.developerdaily:id/edt_phone")
class_name
定位元素find_element_by_class_name
driver.find_element_by_class_name('android.widget.Button')
content-desc
定位元素find_dlement_by_accessibility_id
driver.find_dlement_by_accessibility_id('test')
text
定位元素driver.find_element_by_android_uiautomator("text('xxx')")
driver.find_element_by_android_uiautomator("text('热点')")
xpath
定位元素# 方法一:xpath
xpath = "//*[@resource-id='io.manong.developerdaily:id/tab_layout']//android.widget.RelativeLayout[2]"
driver.find_element_by_xpath(xpath)
# 方法二:xpath+index
xpath = "//*[@resource-id='io.manong.developerdaily:id/tab_layout']//android.widget.LinearLayout//[@index=1]"
driver.find_element_by_xpath(xpath)
# coding=utf-8
from appium import webdriver
import time,traceback
desired_caps = {} # 定义字典:告诉appium我们的一些配置
desired_caps['platformName'] = 'Android' # 固定值
desired_caps['platformVersion'] = '6' # android版本
desired_caps['deviceName'] = 'test' # 连接多个设备时会用到 ?
# desired_caps['app'] = r'/Users/liuhuaiyuan/Downloads/toutiao.apk' # apk在电脑上的路径
desired_caps['appPackage'] = 'io.manong.developerdaily' # apk的package 通过aapt命令查找
desired_caps['appActivity'] = 'io.toutiao.android.ui.activity.LaunchActivity' # 指定apk的启动界面(通常启动界面是唯一的) 通过aapt命令查找
desired_caps['unicodeKeyboard'] = True # 安装中文输入法,以便于控制,及部分中文的输入
desired_caps['resetKeyboard'] = True # 针对上一条语句,如果手机还有其他的输入法,还原输入法,便于其他操作
desired_caps['noReset'] = True # 重要参数,=True 恢复默认值,=False(缺省值) 每次都是从刚安装app的状态进入(格式化状态),可能会显示欢迎页等
desired_caps['newCommandTimeout'] = 6000 # 设置等待时间,当超过这个时间时,将断开与appium的连接。单位是秒
#启动Remote RPC
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps ) # 连接appium,并将参数(字典)传入,执行完此句后,与appium创建一个session
# 0.0.0.0:4723
try:
driver.implicitly_wait(10) # 隐式等待,每0.5秒查询一次,直到指定时间,结束。单位是秒
# 根据id找到元素,并点击,id和 html 元素的id不同
driver.find_element_by_id("io.manong.developerdaily:id/tab_bar_plus").click()
time.sleep(1)
# 定位到密码方式登录
driver.find_element_by_xpath("//*[@resource-id='io.manong.developerdaily:id/tab_layout']//android.widget.RelativeLayout[2]").click()
driver.find_element_by_id("io.manong.developerdaily:id/edt_phone").send_keys('xxxxxx')
driver.find_element_by_id("io.manong.developerdaily:id/edt_password").send_keys('xxxxx')
time.sleep(2)
# 点击登录
driver.find_element_by_id('io.manong.developerdaily:id/btn_login').click()
pass
except:
print (traceback.format_exc())
driver.quit()
driver.find_element_by_accessibility_id()
uia_string:uia_string - The element name in the Android UIAutomator library 使用UIAutomator元素属性名称来定位
driver.find_element_by_android_uiautomator('new UiSelector().resourceId("%s")')
举例:如下图,点击顶部扫码器:
对应uiautomator名称:“resource-id”:
driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.rfchina.app.supercommunity:id/square_title_btn_scan")').click()
选择resource-id 定位需要特别注意,界面中 resource-id 不是唯一的,有可能存在很多控件的resource-id是相同的。
# 根据 text 定位
driver.find_element_by_android_uiautomator('new UiSelector().text("%s")') #对应uiautomator名称:“text”
# 根据 description 定位
driver.find_element_by_android_uiautomator('new UiSelector().description("%s")') # 对应uiautomator名称:“content-desc”
# 根据 className 定位
driver.find_element_by_android_uiautomator('new UiSelector().className("%s")') # 对应uiautomator名称:“class”
# 根据 index 定位
driver.find_element_by_android_uiautomator('new UiSelector().index("%s")') # 对应uiautomator名称:“index”
选择className 定位需要特别注意,界面中 class 往往都不是唯一的,大量控件的class会一样。
driver.find_element_by_xpath()
from appium import webdriver
import time
desired_caps = {
"appPackage": "com.rfchina.app.supercommunity",
"appActivity": "com.rfchina.app.supercommunity.client.StartActivity",
"platformName": "Android",
"deviceName": "Android Emulator"
}
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
time.sleep(5)
driver.find_element_by_id("com.rfchina.app.supercommunity:id/img_serivce_communities_layout").click()
xpath是最常见的定位方法,虽然性能可能是最慢的,但是它是最万能的。 我们可以用模糊匹配来查找
driver.find_element_by_xpath("//android.widget.TextView[@text='speaking']")
获取toast
driver.find_element_by_xpath("//*[contains(@text,‘toast 信息’)]")
这里只介绍了android的定位方法,ios其实也差不多, 其实IOS中的Type, 相当于android中的class, IOS中的name, 相当于android中的text, 其实IOS中的content_desc, 相当于android中的accessbility_id, 这个后面再讲。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有