美文网首页
Python物语:uiautomator 之 Selector

Python物语:uiautomator 之 Selector

作者: 非著名自行车运动员 | 来源:发表于2020-08-07 20:04 被阅读0次

Selector

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

  • text, textContains, textMatches, textStartsWith
  • className, classNameMatches
  • description, descriptionContains, descriptionMatches, descriptionStartsWith
  • checkable, checked, clickable, longClickable
  • scrollable, enabled,focusable, focused, selected
  • packageName, packageNameMatches
  • resourceId, resourceIdMatches
  • index, instance

多重条件进行定位

# 定位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"
  )
  • child_by_description is to find child which or which's grandchild contains the specified description, others are the same as child_by_text.

  • child_by_instance is to find child which has a child UI element anywhere within its sub hierarchy that is at the instance specified. It is performed on visible views without scrolling.

See below links for detailed information:

  • UiScrollable, getChildByDescription, getChildByText, getChildByInstance
  • UiCollection, getChildByDescription, getChildByText, getChildByInstance

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()

相关文章

网友评论

      本文标题:Python物语:uiautomator 之 Selector

      本文链接:https://www.haomeiwen.com/subject/dmetdktx.html