appium元素定位方法总结
使用uiautomatorviewer定位工具driver.find_element_by_android_uiautomator(uia_string)
根据resourceId属性定位
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会一样。
根据content-desc定位 driver.find_element_by_accessibility_id()
根据xpath定位 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()