0

How To Locate Elements using selenium web driver when only Unique value is 'index'? Resource-id,Class and Package are not unique.

Below Layout taken from Android UI Automator

enter image description here

Resource-id = android:id/menuitem

Class =       android.widget.RelativeLayout

Package =     android
1

You can use selenium to find an array of elements that share common characteristics such as class names and etc, then indexing through this array to locate the element you want.

elementArray = webdriver.findelements(xpath / css selector);
theElement = elementArray[1];
  • can you please write x path of this code – lak Nov 25 '16 at 9:57
  • Could you please write full code – lak Dec 19 '16 at 5:21
  • This is a viable solution, but is not as elegant, in my opinion, as BountyHunter's answer, which includes the index in the xpath used to locate the element. – VanderLinden Jan 20 '17 at 15:46
1

You could simply write an xpath with index specified. For the example you mentioned, the xpath should look like:

driver.findElement(By.xpath("(//*[@id='menuitem'])[index]")

where index will start from 1, NOT 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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