Appium Android Python - 使用文本属性查找元素

Appium Android Python - find element using text attribute

我正在尝试使用 driver.find_element_by_name("Now Playing") 查找元素,但它抛出异常。 有没有办法通过属性值查找元素?在我的例子中,属性是 'text' 并且值是 'Now Playing'.

以下是XML画面来源。准确地说,我试图在 'Albums'、'Artists'、'Now Playing'.

的第三个选项卡上 select

非常感谢您的宝贵时间和帮助。

XML 选项卡的内容:

        <android.widget.LinearLayout index="2" package="com.nmp.simp.phone" class="android.widget.LinearLayout" text="" resource-id="com.nmp.simp.phone:id/tab_control" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,1776][1080,1920]" displayed="true">
          <android.widget.LinearLayout index="0" package="com.nmp.simp.phone" class="android.widget.LinearLayout" text="" resource-id="com.nmp.simp.phone:id/bg_tab" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,1776][360,1920]" displayed="true">
            <android.widget.ImageView index="0" package="com.nmp.simp.phone" class="android.widget.ImageView" text="" resource-id="com.nmp.simp.phone:id/icon" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="true" bounds="[127,1778][232,1883]" displayed="true" />
            <android.widget.TextView index="1" package="com.nmp.simp.phone" class="android.widget.TextView" text="Albums" resource-id="com.nmp.simp.phone:id/text" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[135,1874][225,1911]" displayed="true" />
          </android.widget.LinearLayout>
          <android.widget.LinearLayout index="1" package="com.nmp.simp.phone" class="android.widget.LinearLayout" text="" resource-id="com.nmp.simp.phone:id/bg_tab" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[360,1776][720,1920]" displayed="true">
            <android.widget.ImageView index="0" package="com.nmp.simp.phone" class="android.widget.ImageView" text="" resource-id="com.nmp.simp.phone:id/icon" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[487,1778][592,1883]" displayed="true" />
            <android.widget.TextView index="1" package="com.nmp.simp.phone" class="android.widget.TextView" text="Artists" resource-id="com.nmp.simp.phone:id/text" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[504,1874][576,1911]" displayed="true" />
          </android.widget.LinearLayout>
          <android.widget.LinearLayout index="2" package="com.nmp.simp.phone" class="android.widget.LinearLayout" text="" resource-id="com.nmp.simp.phone:id/bg_tab" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[720,1776][1080,1920]" displayed="true">
            <android.widget.TextView index="0" package="com.nmp.simp.phone" class="android.widget.TextView" text="Now Playing" resource-id="com.nmp.simp.phone:id/text" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[826,1874][974,1911]" displayed="true" />
            <android.widget.ImageView index="1" package="com.nmp.simp.phone" class="android.widget.ImageView" text="" resource-id="com.nmp.simp.phone:id/icon" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[847,1778][952,1883]" displayed="true" />
          </android.widget.LinearLayout>
        </android.widget.LinearLayout>

我可以通过以下方法找到具有文本属性的元素:

  1. 搜索名称为 class 的所有元素:

    elements_list = driver.find_elements_by_class_name("android.widget.TextView")

  2. 遍历元素列表,找到'text'属性中具有'Now Playing'的元素。如果找到,请在元素上调用 click()。

    对于 ui_elements 中的元素: 如果 'Now Playing' in str(element.get_attribute("text")): element.click()

我也在找这个,Rak 的回答对我有用。

messages = driver.find_elements_by_class_name('android.widget.TextView')

for i in messages:
    if 'text you are looking for' in i.text:
        i.click()