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:
JavaScript
x
8
1
self.driver.find_element_by_xpath(
2
"xpath=(//[@contentDescription='Cards'])[1]").click()
3
4
time.sleep(0.5)
5
self.assertEqual('Angle x:',
6
self.driver.find_element_by_accessibility_id('MovementsTitle').text)
7
time.sleep(0.5)
8
Thanks!
Advertisement
Answer
You can handle the following way
JavaScript
1
3
1
els = self.driver. find_elements_by_xpath('xpath=(//[@contentDescription='Cards'])')
2
els[0].click()
3
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