Search code examples
xpathappiumxcuitestappium-javaxcuielement

Not able to locate element using following-sibling or following in Appium for Mac


I'm trying to locate the checkbox in the below xml path using xpath following-sibling. But unable to locate the element.

Following is the xml.

    
        <XCUIElementTypeScrollView elementType="46" identifier="_NS:429" label="" title="" enabled="false" selected="false" x="460" y="312" width="521" height="210">
      
            <XCUIElementTypeOutline elementType="29" identifier="_NS:433" label="" title="" enabled="true" selected="false" x="461" y="341" width="519" height="180">
        
                <XCUIElementTypeOutlineRow elementType="30" identifier="" label="" title="" enabled="false" selected="false" x="461" y="341" width="519" height="20">
          
                    <XCUIElementTypeGroup elementType="3" identifier="" label="" title="" enabled="false" selected="false" x="461" y="341" width="300" height="20">
            
                        <XCUIElementTypeDisclosureTriangle elementType="13" identifier="" value="0" label="" title="" enabled="true" selected="false" x="463" y="342" width="13" height="17"/>
            
                        <XCUIElementTypeStaticText elementType="48" identifier="" value="Desktop" label="" title="" enabled="true" selected="false" x="476" y="342" width="284" height="17"/>
          
                    </XCUIElementTypeGroup>
          
                    <XCUIElementTypeCheckBox elementType="12" identifier="" value="2" label="" title="" enabled="true" selected="false" x="762" y="342" width="17" height="17"/>
        
                </XCUIElementTypeOutlineRow>

Tried following xpaths already but not able to locate the element 'XCUIElementTypeCheckBox'.

"//XCUIElementTypeStaticText[@value='Desktop']/following-sibling::XCUIElementTypeCheckBox"

"//XCUIElementTypeOutlineRow[XCUIElementTypeStaticText[@value='Desktop']]/following::XCUIElementTypeCheckBox"

"//XCUIElementTypeStaticText[@value='Desktop']/following::XCUIElementTypeCheckBox"

I'm stuck at this point. As I'm kinda new to Appium for Mac, I'd really appreciate any help in locating the checkbox using xpath or any other way.

NOTE: I haven't attached the entire xml as its too huge.


Solution

  • Following sibling selects element that shares the same parent node, <XCUIElementTypeStaticText and XCUIElementTypeCheckBox aren't siblings.

    Sibling of your element is only XCUIElementTypeGroup , which are sharing XCUIElementTypeOutlineRow as parent.

    Using following-sibling:

    //XCUIElementTypeStaticText[@value='Desktop']/parent::*/following-sibling::XCUIElementTypeCheckBox
    

    or

    //XCUIElementTypeGroup/following-sibling::XCUIElementTypeCheckBox
    

    Using child of parent:

    //XCUIElementTypeOutlineRow/*[2][self::XCUIElementTypeCheckBox]
    

    Modify these xpaths' according to your whole xml document.

    If you will be working with xpaths', you can check out my github project that finds unique xpath selector for you. Any support is greatly appreciated.