前言
环境搭建好了,下一步元素定位,元素定位本篇主要介绍如何使用uiautomatorviewer,通过定位到页面上的元素,然后进行相应的点击等操作.
uiautomatorviewer是android-sdk自带的一个元素定位工具,非常简单好用,使用uiautomatorviewer,你可以检查一个应用的UI来查看应用的布局和组件以及相关的属性。
一、启动uiautomatorviewer.bat
1.打开目录D:\androidsdk\android-sdk-windows\tools

2.双击启动,启动之后出现如下界面

3如果不喜欢双击启动的话,也可以在cmd里面通过指令启动
先cd到tools目录下,然后输入uiautomatorviewer.bat回车后启动服务
二、连接手机
1.cmd打开输入adb devices,确认手机已连上
2.打开手机要使用的APP页面,让屏幕处于点亮状态
3.点左上角安卓机器人按钮Devices Screenshot按钮刷新页面
1.移动鼠标到需要定位的元素上,如搜索输入框


2.右下角可以看到元素对应的属性
text: 空
resource-id: cn.qysxy.daxue:id/iv_home_top_search
class: android.widget.ImageView
xpath: //android.widget.ImageView[@resource-id=‘cn.qysxy.daxue:id/iv_home_top_search‘]
四、点搜索框
1.前面一篇启动app后,休眠五秒,等待页面加载完成
2.通过id来定位到搜索框,然后点击

五、最终代码如下
# 布道官--首页 from appium import webdriver from time import sleep import os caps = {} caps["platformName"] = "Android" # 平台名称 caps["platformVersion"] = "5.1.1" # 设备系统的版本号 caps["deviceName"] = "127.0.0.1:62001" # 夜神虚拟机 caps["appPackage"] = "cn.qysxy.daxue" caps["appActivity"] = ".modules.login.LoginActivity" caps["noReset"] = "true" driver = webdriver.Remote(‘http://127.0.0.1:4723/wd/hub‘, caps) # 隐式等待 ,如果没有找到"布道官"会报错 # driver.implicitly_wait(10) # 获取结果文本 res_text = driver.find_element_by_id(‘cn.qysxy.daxue:id/tv_login_enterprise_name‘).text assert res_text == ‘布道官‘
六、元素定位
1.appium的webdriver提供了11种元素定位方法,在selenium的基础上扩展了三个,可以在pycharm里面输入driver.find_element_by然后会自动匹配出来

2.多的三种如下,在后面的会详细介绍
driver.find_element_by_accessibility_id()
driver.find_element_by_android_uiautomator()
driver.find_element_by_ios_uiautomation()
(第三个是ios的可以暂时不用管)