Two identical AutomationID using xpath python with appium

Tags: , , , ,



In in our mobile app, there are two boxes with the same AutomationId. For automated testing i need to find the first of the two elements by xpath. I tried following code, bt it didn’t work:

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)

Thanks!

Answer

You can handle the following way

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

Description :

First, get the all identical elements through “find_elements” this will give you as an array of elements then you can perform the actions accordingly



Source: stackoverflow