🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## 使用uiautomator定位 `driver.find_element_by_android_uiautomator(uia_string)` *uia_string:uia_string - The element name in the Android UIAutomator library* 使用UIAutomator元素属性名称来定位 ### 根据 resourceId 属性 定位 ``` driver.find_element_by_android_uiautomator('new UiSelector().resourceId("%s")') ``` 举例:如下图,点击顶部扫码器: ![](https://box.kancloud.cn/6e19e34fb58140d741a1a7cd8e7770bc_1221x563.png) **对应uiautomator名称:“resource-id”:** ``` driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.rfchina.app.supercommunity:id/square_title_btn_scan")').click() ``` >[warning] 选择resource-id 定位需要特别注意,界面中 resource-id 不是唯一的,有可能存在很多控件的resource-id是相同的。 ### 根据 text、description、className、index属性定位 ``` # 根据 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” ``` >[warning] 选择className 定位需要特别注意,界面中 class 往往都不是唯一的,大量控件的class会一样。 <br> ## 根据content-desc定位 `driver.find_element_by_accessibility_id()` ## 根据xpath定位 `driver.find_element_by_xpath()` ```python 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() ```