元素定位方式有多种,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. equals方法和==的区别

    equals方法和==的区别   首先大家知道,String既可以作为一个对象来使用,又可以作为一个基本类型来使用.这里指的作为一个基本类型来使用只是指使用方法上的,比如String s = &quo ...

  2. jeecms一些经典标签

    http://www.121ask.com/thread-5512-1.html [@cms_channel_list]标签详细介绍 http://www.jeecmstheme.com/2014/0 ...

  3. C#使用WebService

    一.新建webservice 新建项目→asp.net Web服务应用程序 或者在现有项目中 点击右键 新建web服务程序asmx 只要在webservice类里面 的方法 标注为[WebMethod ...

  4. postman 前置 和 后置 处理器 用法

    基本用法 赋予变量 var  body="我是变量的值" ;   -----给body赋值 postman.setEnvironmentVariable("sign&qu ...

  5. Mysql 备份脚本和window下如何实现自动备份

    @echo offecho.echo      MySQL数据库备份 echo *****************************echo.echo 今天是 %date%echo 时间是 %t ...

  6. Web服务的实质介绍

    web应用的实质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端 Web应用程序是一种可以通过Web访问的应用程序,程序的最大好处是用 ...

  7. probably another instance of uWSGI is running on the same address (127.0.0.1:9090). bind(): Address already in use

    probably another instance of uWSGI is running on the same address (127.0.0.1:9090). bind(): Address ...

  8. leetcode88

    public class Solution { public void Merge(int[] nums1, int m, int[] nums2, int n) { //for (int i = 0 ...

  9. 按键精灵对VBS的支持

    VBSBegin…VBSEnd(VBS块)格式:VBSBegin...VBSEnd用途:可以在VBS块的区域内随意的书写VBS语法指令. 更多说明:由于彻底的转向VBS语言,会导致goto语句不能被兼 ...

  10. //todo 的用处

    在代码中添加 //todo 以后要做的事 可以暂时打上标记,以后再来处理. 光有这个没什么用,关键是IDE要支持,比如VS2017,只要按下 ctrl+w,t 就可以在输出窗口中显示出所有 todo的 ...