user3718160
user3718160

Reputation: 491

xpath with multiple components

I'm trying to find an element that contain multiple ones with xpath for example

<ListView>
  <RelativeLayout>
    <TextView id=text1></TextView>
    <TextView id=text2></TextView>
    <ImageView id=img1></ImageView>
  </RelativeLayout>
  <RelativeLayout>
    <TextView id=text3></TextView>
    <TextView id=text4></TextView>
    <ImageView id=img2></ImageView>
  </RelativeLayout>
</ListView>

I would like to retrieve only the relative layout that contains a textview with id=text1, a textview with id=text2 and an imageview with id=img1

I tried this

//ListView/RelativeLayout/TextView[@resource-id='text1'] | 
//ListView/RelativeLayout/TextView[@resource-id='text2'] | 
//ListView/RelativeLayout/ImageView[@resource-id='img1']

And this

//ListView/RelativeLayout/TextView[@resource-id='text1'] and 
//ListView/RelativeLayout/TextView[@resource-id='text2'] and 
//ListView/RelativeLayout/ImageView[@resource-id='img1']

But none works.

The first one seems to be a select all that contain the 1st OR the 2nd OR the 3rd so not one with the 3 at the same time.

The second one doesn't select aything.

I suppose I should do something like

//ListView/RelativeLayout/TextView[@resource-id='text1'] and TextView[@resource-id='text2'] and ImageView[@resource-id='img1']

But I don't know how to properly write it :/

Any help ?

Upvotes: 1

Views: 508

Answers (1)

har07
har07

Reputation: 89325

"I would like to retrieve only the relative layout that contains a textview with id=text1, a textview with id=text2 and an imageview with id=img1"

You can do this way (wrapped for readability) :

//ListView/RelativeLayout[
            TextView/@resource-id='text1' and 
            TextView/@resource-id='text2' and 
            ImageView/@resource-id='img1'
          ]

or alternatively, something closer to your last attempted XPath :

//ListView/RelativeLayout[
            TextView[@resource-id='text1'] and 
            TextView[@resource-id='text2'] and 
            ImageView[@resource-id='img1']
          ]

Notice that the last element in the main path is the element that would be returned by the XPath, in this case it should be /RelativeLayout, as requested. Also notice that predicate expression ([...]) applied to context node to the left, so the outer predicate in the two XPath above applied to RelativeLayout since the form is /RelativeLayout[....].

Upvotes: 2

Related Questions