Get child elements not recursive

Hello,
I want to get all child elements of an element, but ony from the next level, not their children’s children.
Basically I have the following structure:

RelativeLayout (can be found by id:rootID)
-RelativeLayout Y
--RelativeLayout N
--RelativeLayout N
-RelativeLayout Y
--RelativeLayout N
-RelativeLayout Y

I want to get a list of all elements tagged with Y (only the dircet children)

It can be done by calling:
driver.findElementsByXpath("full/xpath/to/rootIDRelativeLayout/*")
–> this works and I get back the 3 elements with Y

But when trying to get the root Element by ID and then using a relative xPath, it doesn’t works:
driver.findElementsById("app.android.com:id/rootID").findElementsByXpath("./*")
–> the result is an empty list.

When trying the following:
driver.findElementsById("app.android.com:id/rootID").findElementsByXpath(".//*")
–> I get back all RelativeLayouts, also the children’s children. Size: 6

Is there a way of doing this right?

Thank you for your answers! :slight_smile:

1 Like

I found a solution by myself:
@AndroidFindBy(xpath = "//*[@resource-id=\"app.android.com:id/rootID\"]/*")

It works but I am not completely happy with that solution
Is there no way of chainning a findByID with an findByXpath?

1 Like