Get last xpath of specific id

Hi
For a specific object whose xpath is defined as below
//*[@id=“start”]/div[2]/div[2]/div[5]/div[5]/div

This div keeps on repeating like shown below
//[@id=“start”]/div[2]/div[2]/div[5]/div[35]/div
//
[@id=“start”]/div[2]/div[2]/div[5]/div[36]/div

My requirment is how to find the last xpath of the @id=“start” so that we can get the text associated with it.
The xpath is dynamic and from what I have seen the incremental portion can be modified with a variable. My problem is I do not know the dynamic value to set for that variable and so am trying to find the last or max value.
Any suggestions appreciated

Hello,

this would work.

def String findLastElement() {
	String pathTemplate = "//[@id='start']/div[2]/div[2]/div[5]/div[%d]/div"
	int i = 1
	TestObject to = new TestObject()
	to.addProperty('xpath', ConditionType.EQUALS, String.format(pathTemplate, i))
	
    while(WebUI.waitForElementPresent(to, 1)) {
		i++
		to.findProperty("xpath").setValue(String.format(pathTemplate, i))
	}
	
	// last existing index is i-1
	return String.format(pathTemplate, i-1)
}

println findLastElement()