How to extract the word Default from this xml?

问题描述 投票:0回答:2
<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>

经过大量搜索,我从一个更大的 xml 中设法使用以下代码提取了这个较小的部分:

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

我的目标是找出复选标记前面的单词。我可以在 xml 中看到它是

Default
这个词,但我无法在代码中建立它。我使用 Ruby。

你能帮帮我吗?

ruby xml xpath appium nokogiri
2个回答
1
投票

这里是 XPath 表达式,用于选择最后一个具有属性“name”的元素,就在具有属性“name”=“checkmark”的元素之前

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

你可以试试这里

所做的是,选择第一个带有 name=checkmark 的元素(只是为了避免出现多个元素的情况),然后选择其前面的具有 name 的节点并获得第一个(前面的列表以相反的顺序排列)


0
投票

感谢 Exhile79。您的代码给了我节点,从中我得到了“默认”或“自定义”一词。我的代码是这样的:

element_selected = doc.xpath('//*[@name="checkmark"][1]/preceding::*[@name][1]')

if element_selected.attr("value").to_s == "Custom"
  $custom_greeting_activated = true
else
  $custom_greeting_activated = false
end
© www.soinside.com 2019 - 2024. All rights reserved.