rubyxmlxpathappiumnokogiri

How to extract the word Default from this xml?


<XCUIElementTypeCell type="XCUIElementTypeCell" enabled="true" visible="true" x="0" y="165" width="320" height="40">
    <XCUIElementTypeOther type="XCUIElementTypeOther" enabled="true" visible="true" x="0" y="165" width="320" height="1">
    </XCUIElementTypeOther>
    <XCUIElementTypeOther type="XCUIElementTypeOther" enabled="true" visible="true" x="0" y="165" width="320" height="40">
      <XCUIElementTypeOther type="XCUIElementTypeOther" enabled="true" visible="true" x="0" y="165" width="320" height="40">
      </XCUIElementTypeOther>
    </XCUIElementTypeOther>
    <XCUIElementTypeStaticText type="XCUIElementTypeStaticText" enabled="true" visible="true" x="16" y="165" width="258" height="40" name="Default" label="Default" value="Default">
    </XCUIElementTypeStaticText>
    <XCUIElementTypeOther type="XCUIElementTypeOther" enabled="true" visible="true" x="16" y="203" width="304" height="2">
    </XCUIElementTypeOther>
    <XCUIElementTypeButton type="XCUIElementTypeButton" enabled="false" visible="true" x="284" y="176" width="18" height="17" name="checkmark" label="checkmark">
    </XCUIElementTypeButton>
  </XCUIElementTypeCell>

After a lot of searching, from a bigger xml I managed to extract this smaller bit using this code:

  xml = $D[device].page_source
  doc = Nokogiri::XML(xml)
  res = doc.xpath('//*[@name="checkmark"]').first.parent

My goal is to find out what word is preceding the checkmark. I can see in xml that it is the word Default but I am not able to establish this in code. I use Ruby.

Could you help me?


Solution

  • Here is the XPath expression to select the last element that has the attribute "name" just before the element with attribute "name"="checkmark"

    //*[@name="checkmark"][1]/preceding::*[@name][1]
    

    You can try it here

    What is does is, selects the first element with name=checkmark (just to avoid cases where there are more than one), and then selects its preceding nodes that have name and gets the first (preceding list goes in reverse order)