【问题标题】:How to write xpath (get locator) for appium android automation?如何为appium android自动化编写xpath(获取定位器)?
【发布时间】:2020-11-27 18:24:47
【问题描述】:

我正在为 android 应用程序编写 appium 自动化,它没有正确的 xpath、类名或 ID。

即使在图像中,当我单击开始时,您也可以清楚地检查。它点击整个框架。我也附上了 XML。

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<hierarchy index="0" class="hierarchy" rotation="0" width="1080" height="2040">
  <android.widget.FrameLayout index="0" package="com.novo.android.dev" 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,2040]" displayed="true">
    <android.widget.LinearLayout index="0" package="com.novo.android.dev" 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,2040]" displayed="true">
      <android.widget.FrameLayout index="0" package="com.novo.android.dev" 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,60][1080,2040]" displayed="true">
        <android.widget.FrameLayout index="0" package="com.novo.android.dev" 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,60][1080,2040]" displayed="true">
          <android.webkit.WebView index="0" package="com.novo.android.dev" class="android.webkit.WebView" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,60][1080,2040]" displayed="true">
            <android.webkit.WebView index="0" package="com.novo.android.dev" class="android.webkit.WebView" text="Plaid - Securely Connect" checkable="false" checked="false" clickable="false" enabled="true" focusable="true" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,60][1080,2040]" displayed="true">
              <android.view.View index="0" package="com.novo.android.dev" class="android.view.View" text="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[0,60][1080,2040]" displayed="true" />
            </android.webkit.WebView>
          </android.webkit.WebView>
          <android.widget.ProgressBar index="1" package="com.novo.android.dev" class="android.widget.ProgressBar" text="" resource-id="com.novo.android.dev:id/root_progress_bar" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" long-clickable="false" password="false" scrollable="false" selected="false" bounds="[480,990][600,1110]" displayed="true" />
        </android.widget.FrameLayout>
      </android.widget.FrameLayout>
    </android.widget.LinearLayout>
  </android.widget.FrameLayout>
</hierarchy>

【问题讨论】:

标签: xpath automation appium appium-android appium-ios


【解决方案1】:

您要单击的按钮位于android.webkit.WebView 内。换言之,被测应用是一款 Android 混合应用。

要识别 WebView 元素,你应该先切换到 WebView。

用于自动化混合应用程序的 Java 代码:

Set<String> contextNames = driver.getContextHandles();
for (String contextName : contextNames) {
    System.out.println(contextName); //prints out something like NATIVE_APP, WEBVIEW_1
}

// Switching to webview
driver.context(contextNames.toArray()[1]); // set context to WEBVIEW_1

// do some web testing
String myText = driver.findElement(By.cssSelector(".green_button")).click();

// Switching back to NATIVE_APP
driver.context("NATIVE_APP");

// do more native testing if we want
driver.findElement(By.name("hello")).click();

driver.quit();

更多详情,请访问官方文档https://appium.io/docs/en/writing-running-appium/web/hybrid/

【讨论】:

【解决方案2】:

它看起来像一个混合应用程序。您必须在执行时切换到 Web 视图上下文。 查找元素 启动应用程序后,您必须使用远程调试进行检查。 https://developers.google.com/web/tools/chrome-devtools/remote-debugging

【讨论】:

【解决方案3】:

xpath 是: '层次结构/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View'

可以简化为“层次结构//android.view.View”

但错误消息告诉您不要使用 xPath

【讨论】:

    猜你喜欢
    • 2016-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 2020-11-18
    • 2018-03-15
    • 2014-03-25
    相关资源
    最近更新 更多