In this activity, I have 5 Text Views with same resource-id.

How can I select the first one with Java client Appium automation?

Is there any way to use resource-id and index combination.

I expect a non xpath strategy if available to select that.

UI Automator view

  • any specific reason not to use Xpath? If you are not using absolute path, its far more advantageous. Dont you think so? Feb 10, 2017 at 5:06
  • I read that xpath strategy takes more resources that id strategy
    – Sathya R
    Feb 16, 2017 at 4:18

2 Answers 2

  1. Select the Linear layout 1st.
  2. Then using <iList> iWebElement get all the elemants which have id name as Normal_view
  3. After that you will be getting a list of items of Text Views.
  4. Then access what ever the element you want using its index number.

below is a sample ,but in c#

 IList<IWebElement> sidemenuList = elm2.Modules.FindElements(By.TagName("li"));
  • Can you post in Java if possible. Anyway, there is no tag called li in question
    – Sathya R
    Feb 16, 2017 at 4:20
  • yeah so add (By.class("Android.widget.textview"));
    – ChathuD
    Feb 16, 2017 at 5:05

It depends what you are looking for, will you need to use all 5 textViews in your tests or will you only need the 3rd and never use others?

For the former create a list of webElements, then iterate through the list depending on which element you need at the moment. The syntax is pretty easy:

List<WebElement> elementsList = driver.findElements("your locator strategy")
elementsList.get(2).click; //this will click on the highlighted element from your screenshot

For the latter: 1. use either an xpath or UiAutomator locator (both have options to get to a specific element) 2. use page factory and @FindBys annotation to get a chained locator. 3. if the element you're looking for has a unique id - then you can locate it with id, else - don't. If you absolutely want to use id locators, then ask your developers to add unique ids to all elements that you need

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.