0

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.

Harish
  • 306
  • 1
  • 4
  • 14
  • For that sample as posted I think only `//XCUIElementTypeStaticText[@value='Desktop']/following::XCUIElementTypeCheckBox` will select that eement `XCUIElementTypeCheckBox`; the other two expressions don't select anything. – Martin Honnen Mar 05 '23 at 14:06

1 Answers1

1

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.

RifloSnake
  • 327
  • 1
  • 8
  • xpath using chil element worked perfectly! I just wanted to know if I can locate the checkbox with reference to 'XCUIElementTypeStaticText' with value 'Desktop' as I have multiple checkboxes in the xml and I need to differentiate each checkbox. – Harish Mar 06 '23 at 07:16
  • @Harish then you need to get the parent of `XCUIElementTypeStaticText`, and then the following sibling. Check my updated answer. – RifloSnake Mar 06 '23 at 11:34
  • Yes. That updated solution worked. Thanks a lot for the help. I just wanted to clarify one more thing. What does * speifies in the first xpath you mentioned and also, what does *[2] specifiy in one more xpath you mentioned in your answer? One more query is, why do we use self :: in xpath? – Harish Mar 06 '23 at 13:12
  • * means match anything, in `*[2]` means match the second child, that could be any element. `*[2][self::XCUIElementTypeCheckBox]` means select the second child, if only it is of type `XCUIElementTypeCheckBox`. – RifloSnake Mar 06 '23 at 13:17