【问题标题】:Unable to fetch element attribute when using sibling in xpath在 xpath 中使用同级时无法获取元素属性
【发布时间】:2020-05-16 11:00:57
【问题描述】:

我在移动测试中使用 Appium,我想获取元素的文本。我可以使用accessibility-idxpath 获取元素。这是用于获取元素的代码:

firstId='xpath', '//XCUIElementTypeStaticText[@name="5 Roles"]'
secondId= 'accessibility_id', '5 Roles'
thirdId = 'xpath', '//XCUIElementTypeProgressIndicator/following-sibling::XCUIElementTypeOther[1]'

然后我试图获取元素的属性。我的偏好是使用 thirdId 而不是上面提到的其他两个。我正在使用以下代码来获取属性:

    def get_attribute_element(self, element):
        el = self.get_element(element)
        el_value = el.get_attribute('label')
        return el_value

当使用 firstId 和 secondId 时,代码成功地获取了属性并且我的测试通过了,但是当使用 thirdId 时,我的测试失败为 el_value=None 。我不知道为什么当 id 使用兄弟时它返回 None。我在 Appium 检查器中检查了 thirdId,它正确识别了应用程序中的元素,它也是一个唯一的 id。

【问题讨论】:

    标签: xpath automated-tests appium


    【解决方案1】:

    我发现了问题所在。从 Appium 检查器查看时,元素 'xpath', '//XCUIElementTypeProgressIndicator/following-sibling::XCUIElementTypeOther[1]' 很好,但它有一个子元素,该元素具有我想要获取的属性。所以我只需要这个元素的子对象:

    'xpath', '//XCUIElementTypeProgressIndicator/following-sibling::XCUIElementTypeOther[1]/XCUIElementTypeStaticText'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多