def html = """ <html> <head> <title>test</title> </head> <body> <div>div1</div> <div>div2</div> </body> </html>""" def xml = new XmlSlurper().parseText(html) assert xml.body.div[0].text() == "div1" assert xml.body.div[1].text() == "div2"
You can also use collection type methods in div node, e.g. .each / .find, for example:
xml.body.div.find { it.text() == "div2" }
EDIT:
To clarify my answer a bit, given HTML in the same structure as the above sample, but with different content, you can always access the second div using array index 1:
xml.body.div[1]
source share