Python3+Appium学习笔记08-元素定位

appium整合了不同的自动化测试驱动程序。而新版本appium desktop 中安卓是使用UI Automator2来作为驱动程序的。以前版本是使用UI Automator1或 Selendroid。所以参数中需要加上 'automationName': 'uiautomator2'指定驱动程序。第一次使用这个参数的时候,会安装一个sever程序。

另外就是元素操作的时候,需要打开开发者选项中的USB模拟点击权限。

定位方式大部分是和selenium一样的。小部分是安卓和ios对应驱动中特有的。

同样也有find_element(单个元素)和find_elements(多个元素)两种模式,后者定位返回的是一个数组。

 

1)id定位

driver.find_element_by_id()

使用UI Automator Viewer上对应resource-id的值

from appium import webdriver
import time

desired_caps = {
    'platformName': 'Android',
    'platformVersion': '7.1.2',
    'deviceName': '1b6ca8f',
    'noReset': True,
    # 'app':'apk路径',
    'automationName': 'uiautomator2',
    'appPackage': 'com.taobao.taobao',
    'appActivity': 'com.taobao.tao.TBMainActivity'
}
driver = webdriver.Remote('127.0.0.1:4723/wd/hub',  desired_caps)
time.sleep(10)
driver.find_element_by_id("com.taobao.taobao:id/home_searchedit").click()

代码效果是会打开淘宝App,然后点击搜索框

 

2)name定位

driver.find_element_by_name()

使用UI Automator Viewer上对应text的值

在较新的版本已经不能使用name来定位,如果使用name的api来定位会报错

 

3)className定位

driver.find_element_by_class_name()

tab_name定位的方法已被该方法取代

使用UI Automator Viewer上对应的class值,但是一般class值不是唯一的

from appium import webdriver
import time

desired_caps = {
    'platformName': 'Android',
    'platformVersion': '7.1.2',
    'deviceName': '1b6ca8f',
    'noReset': True,
    # 'app':'apk路径',
    'automationName': 'uiautomator2',
    'appPackage': 'com.taobao.taobao',
    'appActivity': 'com.taobao.tao.TBMainActivity'
}
driver = webdriver.Remote('127.0.0.1:4723/wd/hub',  desired_caps)
time.sleep(10)
su = driver.find_elements_by_class_name("android.widget.TextView")
print(len(su))

因为classname基本都不会是唯一的,所以使用find_elements。我这边定位到42个。

 

4.accessibility_id定位

driver.find_element_by_accessibility_id()

官方文档说安卓是用该方法取代name定位方法

使用UI Automator Viewer上对应的content-desc 

from appium import webdriver
import time

desired_caps = {
    'platformName': 'Android',
    'platformVersion': '7.1.2',
    'deviceName': '1b6ca8f',
    'noReset': True,
    # 'app':'apk路径',
    'automationName': 'uiautomator2',
    'appPackage': 'com.baidu.searchbox',
    'appActivity': '.MainActivity'
}
driver = webdriver.Remote('127.0.0.1:4723/wd/hub',  desired_caps)
time.sleep(10)
driver.find_element_by_accessibility_id('产品大全').click()

使用了百度app来演示。效果是打开百度后,点击右上角的产品大全按钮

 

5.xpath定位

driver.find_element_by_xpath()

使用xpath来定位

from appium import webdriver
import time

desired_caps = {
    'platformName': 'Android',
    'platformVersion': '7.1.2',
    'deviceName': '1b6ca8f',
    'noReset': True,
    # 'app':'apk路径',
    'automationName': 'uiautomator2',
    'appPackage': 'com.baidu.searchbox',
    'appActivity': '.MainActivity'
}
driver = webdriver.Remote('127.0.0.1:4723/wd/hub',  desired_caps)
time.sleep(10)
driver.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.TabHost/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout[1]/android.widget.ScrollView/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.TextSwitcher/android.widget.TextView').click()

代码效果是打开百度,然后点击搜索框

官方文档和appium desktop定位工具都提示说尽量不要使用xpath去定位,不稳定。

 

这些方法都是在Native(大致理解手机桌面上的应用)中常用的,还有一些是webview(浏览器应用,或者打开的是个页面,H5这种)中使用的

driver.find_element_by_link_text()
driver.find_element_by_partial_link_text()
driver.find_element_by_css_selector()

如这些是在webview中使用的

 

另外还提供了一些定位的方法

driver.find_element_by_android_data_matcher()
driver.find_element_by_android_viewtag()
driver.find_element_by_custom()
driver.find_element_by_image()

data_matcher  是Espresso驱动程序中DataMatcher定位方式。看源码要去下载插件

viewtag 好像是安卓一个样式标签?

custom 是完全不知道干嘛的

image 是用来定位64位编码的图片。但是好像需要安装一些插件

posted @ 2019-05-15 15:23  梦忆安凉  阅读(979)  评论(0编辑  收藏  举报