Can we pass index value to class in xpath?

Here is my code:

int messageSent=((AppiumDriver)driver).findElements(By.className(“android.widget.RelativeLayout”)).size();

AndroidElement messageSentIndex=(AndroidElement)((AppiumDriver)driver).findElementByXPath("//android.widget.RelativeLayout[4]//android.widget.ImageView");

and I want to use the index captured for class "RelativeLayout[messageSent] in xpath.

@jonahss @sujata_kalluri Please suggest.

I’m assuming this is Java? Shouldn’t that be as simple as:

String xPathStr = String.format ("//android.widget.RelativeLayout[%d]/andriod.widget.ImageView", messageSent);
AndroidElement messageSentIndex = (AndroidElement) ((AppiumDriver)driver).findElementByXPaht(xPathStr);

Is that what you were trying to do?

Thanks @aghoward !

List listOfMessages=driver.findElements(By.id(“com.avaamo.android.staging:id/sent_status”));
int lastMessageIndex=listOfMessages.size();
System.out.println(“Last index is: “+lastMessageIndex);
int i=lastMessageIndex-2;
String XPathForLastMessage=(String.format(”//android.widget.RelativeLayout[%d]//android.widget.LinearLayout//android.widget.ImageView”, i));

***Works fine till the this

but throws “NoSuchElementException” on
AndroidElement lastMessage = (AndroidElement)((AppiumDriver)driver).findElementByXPath(XPathForLastMessage);

Don’t know whats going wrong here.

Ohh, problem resolved. :smile:

@aghoward: Your input has helped me alot, I was not aware of this part… Thanks a lot !