Search code examples
appium

Appium find element by XPath - two elements have same xpath


I have problem testing news up ... I need to getAttribute("name") from page in app so I could compare the title of the news. To locate this title I used:

String storyTitle = driver.findElementByXPath("//android.view.View[@index ='1']").getAttribute("name");

But there is a problem because above this title there is another element that has the same xpath and I get it's content instead of the content I want.

Here is UIAutomator View of those elements:

What I get

Content I would like to get

I need to take that title in content-desc from element so I could compare it with title on previous screen that I pulled out of the news, but I don't know how to skip the first element, and go to the second one, because they have all the options the same exepc content-desc, and I can't use that. Any help?


Solution

  • Use the findElementsByXPath and get the "name" attribute of required occurrence of the element, in your case its 2nd occurrence - get(1).

    String storyTitle = driver.findElementsByXPath("//android.view.View[@index ='1']").get(1).getAttribute("name");