from appium import webdriver
import time
server = 'http://127.0.0.1:4723/wd/hub'
连接设备参数
desired_caps = {
"deviceName": "d9d26a8f",
"platformVersion": "9",
"platformName": "Android",
"appPackage": "com.tencent.mm",
"appActivity": "com.tencent.mm.ui.LauncherUI",
"unicodeKeyboard": "True",
"resetKeyboard": "True",
"noReset": "True"
}
远程遥控
b = webdriver.Remote(server, desired_caps)
元素定位
android_uiautomator
1.android_uiautomator text定位
1.1uiautomator tex精确定位
ele = b.find_element_by_android_uiautomator(‘new UiSelector().text(“通讯录”)’)
ele.click()
1.2uiautomator textContains模糊定位
ele = b.find_element_by_android_uiautomator(‘new UiSelector().textContains(“通讯”)’)
ele.click()
1.3uiautomator textStartsWith文本开头定位
ele = b.find_element_by_android_uiautomator(‘new UiSelector().textStartsWith(“通讯”)’)
ele.click()
1.4uiautomator textMatches正则表达式定位
ele = b.find_element_by_android_uiautomator(‘new UiSelector().textMatches(“通讯.”)’)
ele.click()
-
android_uiautomator resourceId定位,resourceIdMatches定位
ele1 = b.find_element_by_android_uiautomator(‘new UiSelector().resourceId(“com.tencent.mm:id/cn1”)’)
ele1.click() -
android_uiautomator className
class属性一般不唯一,多半用在复数定位,className复数定位–find_elements
ele_list = b.find_elements_by_android_uiautomator(‘new UiSelector().className(“android.widget.ImageView”)’)
print(ele_list)
ele_list[0].click() -
android_uiautomator多属性组合定位,一般组合用id,class,text这三个属性
id+class 属性组合
id_class = ‘resourceId(“com.tencent.mm:id/f8y”).className(“android.widget.ImageView”)’
b.find_element_by_android_uiautomator(id_class).click()
id+text 属性组合
id_text = ‘resourceId(“com.tencent.mm:id/cns”).text(“通讯录”)’
b.find_element_by_android_uiautomator(id_text).click()
android_uiautomator description定位
ele = b.find_element_by_android_uiautomator(‘new UiSelector().description(“更多功能按钮,已折叠”)’)
ele.click()
descriptionStartsWith
b.find_element_by_android_uiautomator(‘new UiSelector().descriptionStartsWith(“更多功能”)’)
descriptionMatches
b.find_element_by_android_uiautomator(‘new UiSelector().descriptionMatches(".*折叠$")’).click()
selenium
1.xpath定位
ele2 = b.find_element_by_xpath(’//*[@resource-id=“com.tencent.mm:id/bhn” and @text=“搜索”]’)
ele2.click()
ele2.send_keys(“缘来是你阿”)
2. id定位
ele3 = b.find_element_by_id(“com.tencent.mm:id/gbv”)
ele3.click()
元素关系定位
- 父子定位childSelector
son = ‘resourceId(“com.tencent.mm:id/g73”).childSelector(resourceId(“com.tencent.mm:id/giv”))’
ele = b.find_element_by_android_uiautomator(son)
ele.click() - 兄弟定位fromParent
brother = ‘resourceId(“com.tencent.mm:id/g73”).childSelector(resourceId(“com.tencent.mm:id/giv”)).fromParent(className(“android.widget.LinearLayout”))’
b.find_element_by_android_uiautomator(brother).click()
description
b.find_element_by_accessibility_id(“更多功能按钮,已折叠”).click()