Search code examples
xpathappiumparent-childresource-id

Trying to Get child node(Switch ) from Parent(LinearLayout) via AndroidFindby(resourceid), Appium


(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)

///////////// You can have a look at the hierarchical representation of the elements below

Hierarchy Image View:

***I have tried to reach the switch button by writing the below code on appium Android for java, but it did not work

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

Solution

  • You using UiSelector syntax in XPath strategy. That is why it isn't working. Try this one:

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

    As you can see here, some boilerplate can be omitted, e.g. new UiSelector().resourceId(...) can be simplified to resourceId(...). One more thing: once you found the root element (LinearLayout with the given resourceId), you can find any child element in the hierarchy using .childSelector() method, nesting doesn't matter.