Python物语:uiautomator 之 Selector

2020-08-07  本文已影响0人  非著名自行车运动员

Selector

Selector supports below parameters. Refer to UiSelector java doc for detailed information.

多重条件进行定位

# 定位resourceId="com.tencent.qqmusic:id/bof且index="0"
d(resourceId="com.tencent.qqmusic:id/bof",index="0").click()

child:子节点

d(resourceId="com.tencent.qqmusic:id/blw").child(index="1").child(index="1").click()

sibling:获取同级节点或者子节点的同级节点
实测发现找到第一级节点后会优先找其子节点,子节点中无符合条件结果则会寻找同级的子节点

d(resourceId="com.tencent.qqmusic:id/cyl").sibling(index="1").click()

child by text or description or instance

# get the child match className="android.widget.LinearLayout"
# and also it or its child or grandchild contains text "Bluetooth"
d(className="android.widget.ListView", resourceId="android:id/list") \
 .child_by_text("Bluetooth", className="android.widget.LinearLayout")

# allow scroll search to get the child
d(className="android.widget.ListView", resourceId="android:id/list") \
 .child_by_text(
    "Bluetooth",
    allow_scroll_search=True,
    className="android.widget.LinearLayout"
  )

See below links for detailed information:

Above methods support chained invoking, e.g. for below hierarchy

<node index="0" text="" resource-id="android:id/list" class="android.widget.ListView" ...>
  <node index="0" text="WIRELESS & NETWORKS" resource-id="" class="android.widget.TextView" .../>
  <node index="1" text="" resource-id="" class="android.widget.LinearLayout" ...>
    <node index="1" text="" resource-id="" class="android.widget.RelativeLayout" ...>
      <node index="0" text="Wi‑Fi" resource-id="android:id/title" class="android.widget.TextView" .../>
    </node>
    <node index="2" text="ON" resource-id="com.android.settings:id/switchWidget" class="android.widget.Switch" .../>
  </node>
  ...
</node>

settings

We want to click the switch at the right side of text 'Wi‑Fi' to turn on/off Wi‑Fi. As there are several switches with almost the same properties, so we can not use like d(className="android.widget.Switch") to select the ui object. Instead, we can use code below to select it.

d(className="android.widget.ListView", resourceId="android:id/list") \
  .child_by_text("Wi‑Fi", className="android.widget.LinearLayout") \
  .child(className="android.widget.Switch") \
  .click()
上一篇 下一篇

猜你喜欢

热点阅读