This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Find xpath that doesn't have specific child

How can I select objects, that don't have a specified child? For example, I need to find objects, that don't have resource-id "btn-add-remove", in example below is container[2].

MobileObject[@resource-id='container']

MobileObject[@resource-id='cardView']

MobileObject[@resource-id='btn-add-remove']

MobileObject[@resource-id='tv_menu_title']

MobileObject[@resource-id='container'][2]

MobileObject[@resource-id='cardView']

MobileObject[@resource-id='btn-add-remove']

MobileObject[@resource-id='tv_menu_title']

The Xpath that I have tried are

- MobileObject[@resource-id='cardView or @resource-id='tv_menu_title' and not @resource-id!='btn-add-remove']/ancestor::MobileObject[@resource-id='container']

- MobileObject[@resource-id='container']//MobileObject[@resource-id='cardView or @resource-id='tv_menu_title' and not @resource-id!='btn-add-remove']

both resulted in container1

  • 0  

    Hi 

    Based on your example:

    //MobileObject[@resource-id='container'][2]//MobileObject[@resource-id!='btn-add-remove']

    or

    //MobileObject[@resource-id='container'][2]//MobileObject[not(@resource-id='btn-add-remove')]

    Let me know if either of these help.

    -Robert

  • 0  

    If you need to find all the objects, you should be able execute a "findAll" with either of the following locators:

    //MobileObject[@resource-id='container']//MobileObject[@resource-id!='btn-add-remove']

    //MobileObject[@resource-id='container']//MobileObject[not(@resource-id='btn-add-remove')]

    Silk Test should return all objects that match the above locators regardless of indexing. The best way to test this is with the locator spy and validating the locator, which should tell you how many objects match the specified locator.

    -Robert

  • 0 in reply to   

    Hi RobertM,

    I've also tried that but still it resulted to container1

  • 0 in reply to   

    Hi RobertM,

    I want to the XPath be able to detect which container doesn't have the btn automatically so I think I could not use index for the container