如何在Android Appium测试中找到嵌套元素?

问题描述 投票:0回答:2

我是Appium的新手。我在网上搜索并尝试了几种方法,但没有任何效果。请找到以下代码:

driver.findElementByXPath("//android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[@resource-id=\"in.dmart:id/linear_guide_view_skip\"]/android.widget.android.widget.TextView").click();

driver.findElementByXPath("//android.widget.LinearLayout[1]/android.widget.RelativeLayout[1]/android.widget.android.widget.TextView[1]").click();

driver.findElementByAndroidUIAutomator("text(\"GOT IT\")").click();

List<WebElement>textView=driver.findElements(By.className("android.widget.android.widget.TextView"));
for(i=0;i<textView.size();i++){    
if(textView.get(i).getText().equals("GOT IT"))
    textView.get(i).click()
}

请找到附带的UIAutomator Viewer截图:

enter image description here

服务器日志:

[debug] [W3C] Calling AppiumDriver.findElements() with args: ["class name","android.widget.android.widget.TextView","e75769f1-53cb-44f7-9094-988b73fbc7be"]
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[debug] [BaseDriver] Waiting up to 0 ms for condition
[debug] [AndroidBootstrap] Sending command to android: {"cmd":"action","action":"find","params":{"strategy":"class name","selector":"android.widget.android.widget.TextView","context":"","multiple":true}}
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"class name","selector":"android.widget.android.widget.TextView","context":"","multiple":true}}
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'android.widget.android.widget.TextView' using 'CLASS_NAME' with the contextId: '' multiple: true
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[CLASS=android.widget.android.widget.TextView]
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements selector:UiSelector[CLASS=android.widget.android.widget.TextView]
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Element[] is null: (0)
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements tmp selector:UiSelector[CLASS=android.widget.android.widget.TextView, INSTANCE=0]
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Failed to locate element. Clearing Accessibility cache and retrying.
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'android.widget.android.widget.TextView' using 'CLASS_NAME' with the contextId: '' multiple: true
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[CLASS=android.widget.android.widget.TextView]
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements selector:UiSelector[CLASS=android.widget.android.widget.TextView]
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Element[] is null: (0)
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] getElements tmp selector:UiSelector[CLASS=android.widget.android.widget.TextView, INSTANCE=0]
[debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":0,"value":[]}
[debug] [W3C] Responding to client with driver.findElements() result: []
[HTTP] <-- POST /wd/hub/session/e75769f1-53cb-44f7-9094-988b73fbc7be/elements 200 13691 ms - 12
automation appium android-testing appium-android
2个回答
0
投票

你可以这样试试。如果根据您的应用程序不同,请编辑className值。

  1. 获取类名的所有元素
  2. 单击文本“GOT IT”的位置 List<WebElement>textView = driver.findElements(By.className("android.widget.TextView")); for(i=0;i<textView.size();i++){ if(textView.get(i).getText().equals("GOT IT")) textView.get(i).click() }

0
投票

通过查看视图层次结构,找到TextView,您无需担心嵌套视图。

您可以尝试使用UiSelector()UIAutomator作为例子。请参阅UiSelector文档here

driver.findElementByAndroidUIAutomator("new UiSelector().text(\""+GOT IT+"\")").click();

更新:

尝试用driver铸造AndroidDriver

((AndroidDriver<MobileElement>)driver).findElementByAndroidUIAutomator("new UiSelector().text(\"GOT IT\")");

© www.soinside.com 2019 - 2024. All rights reserved.