Uiautomator--Uiselector元素定位
一、UiSelector作用
按照一定的条件(例如控件的text值,资源id),定位界面上的元素。UiSelector对象的最终目的是去构造一个UiObject对象。
二、元素定位
1、根据text定位:
| 函数返回值 | 函数体 | 说明 | 用法 |
| UiSelector | text(String text) | 根据“控件text属性的内容”构造出UiSelector对象 | 例如,一个控件text的值是“发现”,UiSelector s = new UiSelector().text("发现"); |
| UiSelector | textContains(String text) | 根据“控件text属性包含的内容”构造出UiSelector对象 | 同上例子:UiSelector s = new UiSelector().textContains("现"); |
| UiSelector | textMatches(String regex) | 根据“控件text属性正则表达式的内容”构造出UiSelector对象 | 正则表达式语法参考网上资料即可。 |
| UiSelector | textStartsWith(String text) | 根据“控件text属性开始的内容”构造出UiSelector对象 | 同上例子:UiSelector s = new UiSelector().textStartsWith("发"); |
比较常用,准确度也比较高,中文查找的时候,有时候text元素是中文的,比如例如,一个控件text的值是“发现”,UiSelector s = new UiSelector().text("发现");此时运行的时候可能会报错 “UiOjbectNotFoundException” ,这时候只要把项目的编码格式改为utf-8就可以了。
2、根据description构造:
| UiSelector | description(String desc) | 根据“控件content-desc属性的内容”构造出UiSelector对象 |
| UiSelector | descriptionContains(String desc) | 包含** |
| UiSelector | descriptionMatches(String regex) | 正则 |
| UiSelector | descriptionStartsWith(String desc) | 以**开始 |
同text的用法基本一致,也是比较靠谱的一种方式。
3、根据资源id:
| UiSelector | resourceId(String id) | 根据资源id获取对象,例如:UiSelector s = new UiSelector().resourceId("com.tencent.mm:id/b8m") |
| UiSelector | resourceIdMatches(String regex) | 根据资源id的正则表达式获取对象 |
4、根据类:
1)UiSelector className(String className):
根据控件的类名来找到UiSelector对象。如图

这时候会出现问题:
因为一般Android布局的时候,同样的控件类名都是一样的。
因此我在微信的登录界面调用: UiSelector s = new UiSelector().className("android.widget.TextView"),它得到的就是我左上开始算第一个class名称为“android.widget.TextView”的控件。
2)UiSelector instance (int instance):
上面提到的假如我们想获取屏幕上电话号码的那个TextView使用这样方法,就可以使用instance:
UiSelector s = new UiSelector().className("android.widget.TextView").instance(1);
3)UiSelector index(int index):
用法和上面的instance差不多,谷歌的原文说这个方法是unreliable的,推荐使用instance方法。
4)UiSelector childSelector(UiSelector selector):
有的时候假如子控件不好获得,而其父控件比较好获得的时候,我们通常采用这样的方式,例如下面:

如图,LinearLayout就是ImageView和EditText的父控件,当子空间text、resource-id为空的时候,这种时候子控件定位比较困难。很明显,父控件id已经给定,那我们就可以先定位到父控件,再定位到子控件这种方法。
在它的父控件的childSelector方法中传入一个带有一定特征的UiSelector对象,即可得到子控件
UiObject wx_input= new UiObject(new UiSelector().className("android.widget.RelativeLayout").childSelector(new UiSelector().className("android.widget.EditText")));
5)UiSelector fromParent(UiSelector selector):
有的时候父控件也不好获得,而是同级的控件(同属一个parent)比较好获取,那么使用这样方法,还拿上面的举例:
我们先得到EditText的UiSelector对象:UiSelector s1 = new UiSelector().resourceId("com.tencent.mm:id/axc");
得到和它同样一个父控件的ImageView的UiSelector对象:UiSelector s2 = fromParent( new UiSelector().className("android.widget.ImageView") );
Uiautomator--Uiselector元素定位的更多相关文章
- 元素定位方法之Uiautomator方法
这个方法只能用于安卓系统,方法通过类UiSelector()来构造对象的 官网地址:https://developer.android.google.cn/topic/libraries/testin ...
- Appium元素定位
一.定位工具(UIAutomator) 运行系统条件: 1)Android Studio 2)系统版本:sdk 要求api18以及以上 打开:Android SDK ---Too ...
- appium---android元素定位
原文链接:https://www.cnblogs.com/meitian/p/6103391.html 一.常用识别元素的工具 uiautomator:Android SDK自带的一个工具,在tool ...
- Robotframework + Appium 之常用元素定位方法
任何自动化测试,其实手动测试也是一样的,元素定位(目标定位)是首要任务,这是最直接的测试对象呀! 好了,废话不多说,又到了元素定位啦,之前我们已经介绍过selenium及appium常用的定位方法,下 ...
- Appium Python 五:元素定位
总结 单个元素定位: driver.find_element_by_accessibility_id(id) driver.find_element_by_android_uiautomator(ui ...
- appium===常用方法介绍,元素定位
https://testerhome.com/topics/3711 元素定位方法: find_element_by_android_uiautomator ,使用uiautomator定位,后面参数 ...
- appium 元素定位与操作:
一.常用识别元素的工具 uiautomator:Android SDK自带的一个工具,在tools目录下 monitor:Android SDK自带的一个工具,在tools目录下 Appium I ...
- Appium Android 元素定位方法 原生+H5
APPIUM Android 定位方式 1.定位元素应用元素 1.1通过id定位元素 Android里面定位的id一般为resrouce-id: 代码可以这样写: WebElement eleme ...
- appium===元素定位
一.常用识别元素的工具 uiautomator:Android SDK自带的一个工具,在tools目录下 monitor:Android SDK自带的一个工具,在tools目录下 Appium Ins ...
- RobotFrameWork+APPIUM实现对安卓APK的自动化测试----第七篇【元素定位介绍】
http://blog.csdn.net/deadgrape/article/details/50628113 我想大家在玩自动化的时候最关心的一定是如何定位元素,因为元素定位不到后面的什么方法都实现 ...
随机推荐
- ES6深入学习记录(一)class方法相关
今天学习class相关的一些使用方法,着重在于class extends class之间可以通过extends关键字实现继承,这比ES5的通过修改原型链实现继承,要清晰和方便很多. 上面的代码定义了一 ...
- ViewStateMode vs EnableViewState
What's the difference between: ViewStateMode: Disabled / Enabled / Inherit EnableViewState: True / F ...
- DedeCMS使用方法----如何将网站上传到服务器
我们如果在本地已经把网站做好了,上传到服务器上去的正确姿势是什么样的呢?简单的很~跟着我的步调来~ 方法一(推荐此方法): 1.把你本地所有的文件压缩,上传至服务器上的根目录,再解压. 2.把本地的数 ...
- 11 Set和Map数据结构
Set和Map数据结构 Set WeakSet Map WeakMap 首先 这四个对象都是 数据结构. 用于存放数据 Set 类似数组. 但是不能重复. 如果你有重复数据,会自动忽略 属性 size ...
- 1. Django系列之Django与ajax上传文件
html代码如下: <div class="form-group"> <label for="exampleInputFile">附件上 ...
- oracle--游标--bai
--复制表 create table emp as(select * from scott.emp); select * from emp; --(1) 最简单的游标 declare --声明并初始化 ...
- 19个必须知道的Visual Studio快捷键
项目相关的快捷键 Ctrl + Shift + B = 生成项目 Ctrl + Alt + L = 显示Solution Explorer(解决方案资源管理器) Shift + Alt+ C = 添加 ...
- GetViewUrl
<f:TemplateField HeaderText="名称" ColumnID="Name" ExpandUnusedSpace="true ...
- easyui如何动态改变列的编辑属性
动态改变列的编辑属性 var tt=$('#dg').datagrid('getColumnOption', 'yearContent'); //通过列名获得此列 tt.editor={type:'t ...
- 本地无法启动MySQL服务,报的错误:1067,进程意外终止
在本地计算机无法启动MYSQL服务错误1067进程意外终止 这种情况一般是my.ini文件配置出错了 首先找到这个文件: 默认安装路径 C:/Program Files/MySQL/MySQL Ser ...