一、介绍

他是使用安卓自带的UiAutomator测试框架来实现定位,包含一个名称是UiSelector的类,专门用于在安卓App界面上选择界面元素。因为他是Java开发实现的,所以必须要使用双信号,仅适用于Android App中元素的定位。
二、语法

  1. # 因为参数如果是字符串类型,必须使用Java语法里的双引号,所以python程序外层的引号就要用单引号。
  2. # 语法一
  3. driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,'new UiSelector().方法("参数")')
  4. # 语法二:省略new UiSelector()
  5. driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,'方法("参数")')

三、方法-参数

text

参数 截图
text(“完整字符串”):text属性值精确匹配。 image.png
```python

driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,’text(“9”)’).click()

  1. | |
  2. <a name="jLHdG"></a>
  3. #### description
  4. | 参数 | 截图 |
  5. | --- | --- |
  6. | description("content-desc中完整字符串"):content-desc属性值精确匹配。
  7. | ![image.png](https://cdn.nlark.com/yuque/0/2023/png/25474243/1672838304473-17167ac7-a2ad-46a4-a0cc-e7bbaf813e4c.png#averageHue=%23dededd&clientId=u4ff4fb89-c1ba-4&from=paste&height=555&id=u5936a664&originHeight=694&originWidth=1283&originalType=binary&ratio=1&rotation=0&showTitle=false&size=77542&status=done&style=none&taskId=u0f28a2b7-1d4e-4c22-ae09-a2ee775b265&title=&width=1026.4) |
  8. | ```python
  9. driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,'description("加")').click()

| |

resourceId

参数 截图
resourceId(“resource-id属性值的完整字符串”):resource-id属性值精确匹配。

| image.png | | ```python driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,’resourceId(“com.android.calculator2:id/digit_3”)’).click()

  1. | |
  2. <a name="ZDZ3d"></a>
  3. #### className
  4. | 参数 | 截图 |
  5. | --- | --- |
  6. | className("class属性值的完整字符串"):class属性值精确匹配。一般用于类名特殊的情况下,比如当前App界面上此类型元素只有一个,就可以使用className方法定位。 | ![image.png](https://cdn.nlark.com/yuque/0/2023/png/25474243/1672838805883-871abef6-3f23-4447-a990-8ff9dcd5ae13.png#averageHue=%23f2f2f2&clientId=u4ff4fb89-c1ba-4&from=paste&height=670&id=uf12384bf&originHeight=838&originWidth=1574&originalType=binary&ratio=1&rotation=0&showTitle=false&size=117671&status=done&style=none&taskId=uab68a74c-3369-4396-a155-95b5fbdc3ce&title=&width=1259.2) |
  7. | ```python
  8. driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,'className("android.widget.EditText")').clear()

| |

index

参数 截图
index(index属性值):index属性值的参数是整数类型的数据。 —-一般用于索引号较大的情况下使用。 image.png
```python

driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,’className(“android.widget.EditText”)’).clear()

  1. | |
  2. <a name="VHrAE"></a>
  3. #### textContains
  4. | 参数 | 截图 |
  5. | --- | --- |
  6. | textContains("text属性值里包含的一部分字符串"):text属性值模糊匹配。
  7. | ![image.png](https://cdn.nlark.com/yuque/0/2023/png/25474243/1672838967700-7be00069-c3c2-4d30-87e0-09a799f33963.png#averageHue=%23c1c1c1&clientId=u4ff4fb89-c1ba-4&from=paste&height=671&id=ud9895564&originHeight=839&originWidth=1560&originalType=binary&ratio=1&rotation=0&showTitle=false&size=159093&status=done&style=none&taskId=uc19296be-c0b4-4e3e-80e5-ae17dc4632c&title=&width=1248) |
  8. | ```python
  9. driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,'textContains("Invisible")').click()

| |

textStartsWith

参数 截图
textStartsWith(“text属性值里开头的一部分字符串”):text属性值模糊匹配。

| image.png | | ```python driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,’textStartsWith(“Add”)’).click()

  1. | |
  2. <a name="Gctxh"></a>
  3. #### textMatches
  4. | 参数 | 截图 |
  5. | --- | --- |
  6. | textMatches("text属性值字符串所匹配的正则表达式"):text属性值功能最强大的模糊匹配。 | ![](https://cdn.nlark.com/yuque/0/2023/png/25474243/1672838967700-7be00069-c3c2-4d30-87e0-09a799f33963.png#averageHue=%23c1c1c1&from=url&id=MKjJl&originHeight=839&originWidth=1560&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=) |
  7. | ```python
  8. driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,'textMatches("Show.*Only.*")').click()

| |

descriptionContains

参数 截图
descriptionContains(“content-desc属性值所包含的字符串”) image.png
```python

driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,’descriptionContains(“Invisible”)’).click()

  1. | |
  2. <a name="CK8hr"></a>
  3. #### descriptionStartsWith
  4. | 参数 | 截图 |
  5. | --- | --- |
  6. | descriptionStartsWith("content-desc属性值开头的字符串")
  7. | ![image.png](https://cdn.nlark.com/yuque/0/2023/png/25474243/1672839343303-65c03dfa-ad8d-409e-9320-1a1a3cdbfef6.png#averageHue=%23f0f0ef&clientId=u4ff4fb89-c1ba-4&from=paste&height=658&id=ud8a56fdd&originHeight=823&originWidth=1566&originalType=binary&ratio=1&rotation=0&showTitle=false&size=237041&status=done&style=none&taskId=u210c1dcd-3e7c-4538-8356-30ab440c682&title=&width=1252.8) |
  8. | ```python
  9. driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,'descriptionStartsWith("手机号")').clear()

| |

descriptionMatches

参数 截图
descriptionMatches(“content-desc属性值字符串所匹配的正则表达式”) Android_UiAutomator - 图6
```python

driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,’descriptionMatches(“手机.*框”)’).send_keys(‘12345’)

  1. | |
  2. <a name="VFH9O"></a>
  3. #### resourceIdMathes
  4. | 参数 | 截图 |
  5. | --- | --- |
  6. | resourceIdMathes("resource-id属性值字符串所匹配的正则表达式"):resource-id属性值唯一的模糊匹配,resource-id属性值没有其他模糊匹配方法。 | ![](https://cdn.nlark.com/yuque/0/2023/png/25474243/1672839343303-65c03dfa-ad8d-409e-9320-1a1a3cdbfef6.png?x-oss-process=image%2Fresize%2Cw_937%2Climit_0#averageHue=%23f0f0ef&from=url&id=Xg2Qv&originHeight=492&originWidth=937&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=) |
  7. | ```python
  8. driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,'resourceIdMatches(".*OK")').click()

| |

classNameMatches

参数 截图
classNameMatches(“class属性值字符串所匹配的正则表达式”):class属性值唯一的模糊匹配,class属性值没有其他模糊匹配方法。 image.png
```python

driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,’classNameMatches(“.*EditText”)’).clear()

  1. | |
  2. <a name="sHrus"></a>
  3. #### instance
  4. | 参数 | 截图 |
  5. | --- | --- |
  6. | instance(前面搜索结果里的索引号) : 必须结合前面的方法进行使用,在前面的方法搜索出来的所有结果里进行排序,从0开始编号,按照此编号来找到目标元素。<br />说明:instance方法写在其他方法后面,用一个点分隔开。注意:instance的索引号要与元素的index属性值不同,我们自己数出来第几个(类似于Selenium中XPATH定位的索引号,先用前面的条件过滤,然后把过滤后的结果排序),index属性值是源代码中能看到的代表父节点下所有子节点的排序索引号(类似于Selenium中CSS定位中的索引号,是不论什么标记节点,只要是同父都一起排序) | ![image.png](https://cdn.nlark.com/yuque/0/2023/png/25474243/1672839688702-28c23149-7a90-4c1a-8917-74135fb1dd1a.png#averageHue=%23f1f0ef&clientId=u4ff4fb89-c1ba-4&from=paste&height=665&id=u75265e0e&originHeight=831&originWidth=1564&originalType=binary&ratio=1&rotation=0&showTitle=false&size=201766&status=done&style=none&taskId=ub80e999b-edd4-48dd-95eb-8ccb986fa87&title=&width=1251.2)
  7. |
  8. | ```python
  9. driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,'className("android.widget.Button").instance(0) ').clear()

| |

focused(true)

参数 截图
focused(true) —-有焦点的元素 image.png
```python

driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,’focused(true)’).clear()

  1. | |
  2. | 参数 | 截图 |
  3. | --- | --- |
  4. | checked(true).instance(1) ---当前界面上被选中的元素里的第2个 | ![image.png](https://cdn.nlark.com/yuque/0/2023/png/25474243/1672839906645-c7761fcb-fb67-487d-9e3f-b94ef0113d6b.png#averageHue=%23f0efef&clientId=u4ff4fb89-c1ba-4&from=paste&height=663&id=u6e3b192d&originHeight=829&originWidth=1568&originalType=binary&ratio=1&rotation=0&showTitle=false&size=228105&status=done&style=none&taskId=u5ab01030-9a44-408f-a164-f22da9a0d98&title=&width=1254.4) |
  5. | ```python
  6. driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,'checked(true).instance(1)').click()

| |

组合

参数 截图
多个条件组合选择:链式写法
1.语法:方法1(参数1).方法2(参数2).方法3(参数3)……
2.分隔符:一个点
3.功能:在前一个方法查找到结果里,再次进行下一个方法的查找,如果这些方法里有instance方法,那么instance方法位于末尾。
image.png
```python

driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,’className(“android.widget.CheckBox”).text(“擅长体育”).resourceIdMatches(“.*sports”)’).click()

  1. | |
  2. <a name="UjyB2"></a>
  3. #### 节点
  4. | 参数 | 截图 |
  5. | --- | --- |
  6. | 父.childSelector(目标子) ---按照父子关系查找,可以多重嵌套<br />多重嵌套:祖父.childSelector(父.childSelector(目标子))曾祖父.childSelector(祖父.childSelector(父.childSelector(目标子)))<br />说明:各个层级都可以使用链式写法进行单个条件筛选或多个条件组合筛选。 | |
  7. | ```python
  8. driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,'classNameMatches(".*RadioGroup").childSelector(index(1))').click()

| |