元素定位方法之Uiautomator方法

这个方法只能用于安卓系统,方法通过类UiSelector()来构造对象的

官网地址:https://developer.android.google.cn/topic/libraries/testing-support-library/index.html#UIAutomator

打开链接到UiSelectorhttps://developer.android.com/reference/android/support/test/uiautomator/UiSelector.html

定位方法:driver.find_element_by_android_uiautomator('new UiSelector().text("9")') 返回元素对象

通过元素文本进行定位:

1、根据文本定位

  text(String text)

  new UiSelector().text("9")

2、模糊匹配文本

  textContains(文本)

3、以某个文本开头来匹配

  textStartsWith(文本)

4、正则匹配

  textMatches(正则表达式)

  new UiSelector().textMatches("^D.*")

根据resourceId定位元素:

  resourceId(id)

  new UiSelector().resourceId("com.ibox.calculators:id/digit6")

根据className定位元素:

  注意:className不是唯一的,在Android页面为元素的类型

  new UiSelector().className("android.widget.Button")

根据contenet-des属性定位:

  description(contenet-des属性值)

  new UiSelector().description("cramp fast")

组合定位:

  className 和text组合定位

  new UiSelector().className("android.widget.TextView").text("上升最快")

  其他类推

根据元素关系定位:

  1、后代元素定位

    使用条件:子元素属性不定,不唯一,只能通过父元素来定位

    通过父元素来找后代元素,不一定是父子关系

    new UiSelector().resourceId("com.ibox.calculators:id/cv").childSelector(className("android.widget.TextView"))

    new UiSelector().resourceId("io.manong.developerdaily:id/tab_bar").childSelector(text("我的"))

2、兄弟元素定位

  使用条件:兄弟元素定位容易

  通过子元素找到父元素,然后通过父元素再去找兄弟元素

  new UiSelector().resourceId(id).fromParent(text("9"))

instance与index区别:

都是从0开始计数,但是:

其中instance是匹配的结果所有元素里面 的第几个元素

index则是其父元素的几个节点类似xpath 里面的*[n]

instance用法:

xml:

new UiSelector().resourceId("io.manong.developerdaily:id/tab_bar").childSelector(className("android.widget.TextView").instance(3))

原文地址:https://www.cnblogs.com/aiyumo/p/11858125.html