元素定位方式有多种,Android也有自身独有的定位方式。下面就单独介绍其基于uiautomator定位元素的方法:

基本语法:

driver.find_element_by_android_uiautomator(xx)

1).通过text文本定位语法

  new UiSelector().text("text文本")

#text
loc_text = 'new UiSelector().text("图书")'
driver.find_element_by_android_uiautomator(loc_text).click()

2).如果文本比较长,可以用textContains模糊匹配

  new UiSelector().textContains("包含text文本")

# textContains
loc_textContains = 'new UiSelector().textContains("图")'
driver.find_element_by_android_uiautomator(loc_textContains).click()

3).同样可以用textStartsWith是以某个文本开头来匹配

  new UiSelector().textStartsWith("以text文本开头")

#textStartsWith
loc_textStart = 'new UiSelector().textStartsWith("图")'
driver.find_element_by_android_uiautomator(loc_textStart).click()

4).也可以用正则表达式textMatches匹配

  new UiSelector().textMatches("正则表达式")

2.resourceId

与by_id一样

  new UiSelector().resourceId("id")

#resourceId
loc_id = 'new UiSelector().resourceId("com.baidu.yuedu:id/webbooktitle")'
driver.find_element_by_android_uiautomator(loc_id).click()

3.className

页面上的class属性一般不唯一,多半用在复数定位时候。此时定位相应下标

  new UiSelector().className("className")

#className复数定位
loc_class = 'new UiSelector().className("android.widget.TextView")'
driver.find_elements_by_android_uiautomator(loc_class)[2].click()

 4.description

也是用contenet-des属性定位

  new UiSelector().description("contenet-des属性")

5.组合定位

1).id与text属性组合

#id+text
id_text = 'resourceId("com.baidu.yuedu:id/webbooktitle").text("小说")'
driver.find_element_by_android_uiautomator(id_text).click()

2).class与text属性组合

#class+text
class_text = 'className("android.widget.TextView").text("图书")'
driver.find_element_by_android_uiautomator(class_text).click()

6.关系定位

1).父子定位childSelector

  有时候不能直接定位某个元素,但是它的父元素很好定位,这时候就先定位父元素,通过父元素找儿子

如上定位书架:

#父子关系childSelector
son = 'resourceId("com.baidu.yuedu:id/rl_tabs").childSelector(text("小说"))'
driver.find_element_by_android_uiautomator(son).click()

2).兄弟定位fromParent

  有时候父元素不好定位,但是跟他相邻的兄弟元素很好定位,这时候就可以通过兄弟元素,找到同一父级元素下的子元素

如上定位书架:

#兄弟关系fromParent
brother = 'resourceId("com.baidu.yuedu:id/lefttitle").fromParent(text("图书"))'
driver.find_element_by_android_uiautomator(brother).click()

Appium之uiautomator定位元素的更多相关文章

  1. appium 使用findElementByAndroidUIAutomator 定位元素示例

    appium 使用findElementByAndroidUIAutomator 定位元素示例 import io.appium.java_client.remote.MobileCapability ...

  2. Appium+Python3+iOS定位元素

    前言: 最近在做IOS自动化测试,IOS的Appium环境都配置OK,执行起来真的慢,慢到怀疑人生,那么今天就来总结一下IOS定位方式和各个定位方式的速度排序. 据我观察,按查找元素的顺序速度,从快到 ...

  3. Appium 使用android_uiautomator定位元素时报错: The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource

    使用 android_uiautomator 定位元素时(现在用的还不太熟,对于这个方法还需要加深了解)报错: 报错信息:The requested resource could not be fou ...

  4. 【appium】根据UIAutomator定位元素

    text属性的方法 driver.find_element_by_android_uiautomator('new UiSelector().text("Custom View") ...

  5. 【appium】根据UIAutomator定位元素等等资料

    https://www.cnblogs.com/paulwinflo/p/4742529.html http://www.cnblogs.com/meitian/p/6103391.html http ...

  6. Appium之xpath定位元素

    原文:http://www.cnblogs.com/cnkemi/p/9180525.html appium也是以webdriver为基的,对于元素的定位也基本一致,只是增加一些更适合移动平台的独特方 ...

  7. uiautomator定位元素

  8. Python+Appium自动化测试(9)-自动选择USB用于传输文件(不依赖appium对手机页面元素进行定位)

    一,问题 app自动化测试使用Android真机连接电脑时,通常会遇到两种情况: 1.测试机连接电脑会弹窗提示USB选项,选择USB用于"传输文件",有些手机不支持设置默认USB选 ...

  9. Appium脚本(4) 使用uiautomator方法定位元素

    from app.find_element.capability import driver from time import sleep # 使用uiautomator方法定位元素 accunt_i ...

随机推荐

  1. 02 - Unit04:笔记本加载功能

    @ExceptionHandler 在控制器中统一处理异常. 为了重用异常处理方法,可以将处理方法抽象到父类中,子类共享异常处理方法. 语法: @ExceptionHandler(Exception. ...

  2. WIN10下搭建react-native开发Android环境

    最近公司要求使用react-native进行移动端开发,据说macOS上开发坑会少的多,但我们是windows,莫法,直接抗吧!周末配置环境遇到很多问题,谨以此文做个记录... 准备 安装Chocol ...

  3. Java获取Resource目录下的文件

    工程结构: 有两种方式: Java代码中的类,要获取Resource资源文件目录下文件 绝对路径寻址 String s1 = this.getClass().getResource("/te ...

  4. nodejs+express+ejs+mongoose实例

    nodejs+express+ejs+mongoose实例 nodejs学得异常痛苦,在这里将学的东西做一番整理,算是自我安慰吧.根据网上todo示例,用express和mongoose重写了部分代码 ...

  5. javascript客户端遍历控件与获取父容器对象

    javascript客户端遍历控件与获取父容器对象示例代码 1,遍历也面中所有的控件function findControlAll()    {        var inputs=document. ...

  6. c++builder 字节 编码 转换大全 String TBytes byte

    System.SysUtils System::DynamicArray<System::WideChar> TCharArray System::TArray__1<System: ...

  7. javax.persistence.RollbackException: Error while committing the transaction

    the valid jpa update entity code gives the exception below in the case of  wrong dependency( org.hib ...

  8. Zabbix 监控 Cisco ASA5525 流量

    简介: Zabbix 监控 Cisco ASA5525 网络接口流量 一.Zabbix 支持 SNMP.Cisco 开启 SNMP 二.测试 shell > snmpwalk -v 2c -c ...

  9. C语言结合汇编开发系统内核

  10. 78. Subsets (Back-Track, DP)

    Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...