如何使用XPath根据其子元素查找父元素

时间:2019-12-08 13:47:19

标签: android xpath android-uiautomator appium-android

我正在创建一个函数来返回给定元素的XPath。为此,我需要找到一种获取给定子元素的父元素的方法。

我想知道是否有办法做到这一点:

{
  "id": "1",
  "firstName": "Min",
  "lastName": "Heo"
}

想法是基于已知的子元素获取父元素。

使用WebElement时可以正常工作,但使用AndroidElement时则无法工作。

编辑 这是页面的XML:

AndroidElement parentElement = childElement.findElement(By.xpath("SOME_XPATH_value"));

例如,我想获取以下元素的父元素:

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<hierarchy index="0" class="hierarchy" rotation="0" width="1080" height="1812">
  <android.widget.FrameLayout index="0" package="io.testproject.demo" class="android.widget.FrameLayout" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,0][1080,1812]" displayed="true">
    <android.widget.LinearLayout index="0" package="io.testproject.demo" class="android.widget.LinearLayout" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,0][1080,1812]" displayed="true">
      <android.widget.FrameLayout index="0" package="io.testproject.demo" class="android.widget.FrameLayout" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,72][1080,1065]" displayed="true">
        <android.view.ViewGroup index="0" package="io.testproject.demo" class="android.view.ViewGroup" text="" resource-id="io.testproject.demo:id/decor_content_parent" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,72][1080,1065]" displayed="true">
          <android.widget.FrameLayout index="0" package="io.testproject.demo" class="android.widget.FrameLayout" text="" resource-id="io.testproject.demo:id/action_bar_container" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,72][1080,240]" displayed="true">
            <android.view.ViewGroup index="0" package="io.testproject.demo" class="android.view.ViewGroup" text="" resource-id="io.testproject.demo:id/action_bar" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,72][1080,240]" displayed="true">
              <android.widget.TextView index="0" package="io.testproject.demo" class="android.widget.TextView" text="TestProject Demo" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[48,115][526,196]" displayed="true" />
            </android.view.ViewGroup>
          </android.widget.FrameLayout>
          <android.widget.FrameLayout index="1" package="io.testproject.demo" class="android.widget.FrameLayout" text="" resource-id="android:id/content" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,240][1080,1065]" displayed="true">
            <android.view.ViewGroup index="0" package="io.testproject.demo" class="android.view.ViewGroup" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,240][1080,1065]" displayed="true">
              <android.widget.ScrollView index="0" package="io.testproject.demo" class="android.widget.ScrollView" text="" resource-id="io.testproject.demo:id/scrollView" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[24,240][1056,874]" displayed="true">
                <android.widget.LinearLayout index="0" package="io.testproject.demo" class="android.widget.LinearLayout" text="" resource-id="io.testproject.demo:id/name_components" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[24,240][1056,874]" displayed="true">
                  <android.widget.TextView index="0" package="io.testproject.demo" class="android.widget.TextView" text="Full Name" resource-id="io.testproject.demo:id/labelFullName" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[24,240][1056,297]" displayed="true" />
                  <android.widget.EditText index="1" package="io.testproject.demo" class="android.widget.EditText" text="Enter your Full Name" resource-id="io.testproject.demo:id/name" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="true" long-clickable="true" password="false" scrollable="false" selected="false" bounds="[24,297][1056,433]" displayed="true" />
                  <android.widget.TextView index="2" package="io.testproject.demo" class="android.widget.TextView" text="Password" resource-id="io.testproject.demo:id/labelPassword" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[24,433][1056,490]" displayed="true" />
                  <android.widget.EditText index="3" package="io.testproject.demo" class="android.widget.EditText" text="Enter your password" resource-id="io.testproject.demo:id/password" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="true" password="true" scrollable="false" selected="false" bounds="[24,490][1056,628]" displayed="true" />
                  <android.widget.TextView index="4" package="io.testproject.demo" class="android.widget.TextView" text="Hint: password is 12345" resource-id="io.testproject.demo:id/labelPasswordHint" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[24,628][1056,685]" displayed="true" />
                  <android.widget.Button index="5" package="io.testproject.demo" class="android.widget.Button" text="LOGIN" resource-id="io.testproject.demo:id/login" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[24,730][1056,874]" displayed="true" />
                </android.widget.LinearLayout>
              </android.widget.ScrollView>
            </android.view.ViewGroup>
          </android.widget.FrameLayout>
        </android.view.ViewGroup>
      </android.widget.FrameLayout>
    </android.widget.LinearLayout>
  </android.widget.FrameLayout>
</hierarchy>

应该是:

<android.widget.TextView index="0" package="io.testproject.demo" class="android.widget.TextView" text="Full Name" resource-id="io.testproject.demo:id/labelFullName" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[24,240][1056,297]" displayed="true" />

1 个答案:

答案 0 :(得分:0)

尝试将以下内容用作xpath表达式,并查看其是否有效:

//hierarchy//*[local-name()='android.widget.TextView'][@resource-id="io.testproject.demo:id/labelFullName"]/parent::*

或者只是

//hierarchy//android.widget.TextView[@resource-id="io.testproject.demo:id/labelFullName"]/parent::*