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 我想大家在玩自动化的时候最关心的一定是如何定位元素,因为元素定位不到后面的什么方法都实现 ...
随机推荐
- freemarker种种
#include要使用绝对路径的话,在路径最前面加个"/"就行--<#include "/includes/v2/headerMacro.ftl"> ...
- nginx代理TCP端口
1.升级nginx 版本至1.9.0以上 升级流程参考 nginx平滑升级 2.配置编译的时候需要加上 ./configure --prefix=/usr/local/nginx --user=www ...
- JFinalConfig配置
package com.sandu.common.config; import com.jfinal.config.Constants; import com.jfinal.config.Handle ...
- iOS - 跳到系统App内部设置
从App中跳转到手机设置中此App内的设置授权界面: NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if ...
- 解决Windows版Git出现templates not found的问题
环境: Win10 x64 Git windows客户端(下载自 https://git-scm.com/) SourceTree 1.9.6.1(使用系统安装的Git,而非SourceTree内嵌的 ...
- 解决virtualbox装ghost xp装驱动时报portcls.sys蓝屏的问题
原因:portcls.sys是声卡驱动文件,驱动与模拟的声卡型号不匹配. 解决办法:进入PE,把C:\sysprep\audio目录删除.进入系统后用驱动精灵装驱动.
- [Unreal]学习笔记001
常规 为了便于控制,创建自己的Gamemode和Controller,再通过Setting->World Setting进行设置 在场景中,按住鼠标右键来移动视角,按住右键的同时按下W/S,控制 ...
- git操作---更新删除
1.更新git git pull <远程主机名> <远程分支名> 例如:git pull origin master 2.更新子模块 git submodule updat ...
- js正则表达式的一些研究,截取两个字符串中间的字符串
一个最常用的场景 截取两个字符串中间的字符串 var str = "iid0000ffr"; var substr = str.match(/id(\S*)ff/); ...
- 在 .NET 中开发基于 Chrome 内核的浏览器-创建一个简单浏览器
首先在 http://www.cftea.com/tools/downloads/Cef.zip 下载文件包. 一.将文件解压拖入到 Visual Studio 对应的 WinForm 项目中. 二. ...