两个相同的 AutomationID 使用 xpath python 和 appium

Two identical AutomationID using xpath python with appium

在我们的移动应用程序中,有两个盒子具有相同的 AutomationId。 对于自动化测试,我需要通过 xpath 找到两个元素中的第一个。 我尝试了以下代码,但是它没有用:

self.driver.find_element_by_xpath(
        "xpath=(//[@contentDescription='Cards'])[1]").click()

time.sleep(0.5)
self.assertEqual('Angle x:',
        self.driver.find_element_by_accessibility_id('MovementsTitle').text)
time.sleep(0.5)

谢谢!

您可以通过以下方式处理

    els = self.driver. find_elements_by_xpath('xpath=(//[@contentDescription='Cards'])')
    els[0].click()

描述:

首先,通过"find_elements"得到所有相同的元素,这将给你一个元素数组,然后你可以相应地执行操作

如果他们有相同的id,你可以这样做,使用findelements方法(复数是)如下:

driver.findElementsByAccessibilityID("Cards").get(0).click();

只需指定元素的索引尝试 0 或 1。