首页 > 解决方案 > 尝试通过 AndroidFindby(resourceid)、Appium 从 Parent(LinearLayout) 获取子节点(Switch)

问题描述

(class=android.widget.LinearLayout,resource-id=settings_language_selection_toggle,index=1 ) 

      (class=android.widget.RelativeLayout,index=0)

      (class=android.widget.LinearLayout,resource-id=widget_frame, index=1)

            (class=android.widget.Switch,resource-id=switchWidget, index=0)

//////////// 你可以看看下面元素的分层表示

层次图像视图:

***我试图通过在appium Android for java上编写以下代码来达到开关按钮,但它不起作用

@AndroidFindBy(xpath ="new UiSelector().resourceId(\"com.idscan.mjcs.sample:id/settings_language_selection_toggle\").instance(1).getChildById(new UiSelector().className(\"android.widget.Switch\")

标签: xpathappiumparent-childresource-id

解决方案


您在 XPath 策略中使用 UiSelector 语法。这就是为什么它不起作用。试试这个:

@AndroidFindBy(uiAutomator = "resourceId(\"settings_language_selection_toggle\").childSelector(className(\"android.widget.Switch\"))")

正如您在此处看到的,可以省略一些样板,例如new UiSelector().resourceId(...)可以简化为resourceId(...). 还有一件事:一旦找到根元素(具有给定 resourceId 的LinearLayout),就可以使用方法找到层次结构中的任何子元素.childSelector(),嵌套无关紧要。


推荐阅读