AZMorgani AZMorgani - 5 months ago 8
Java Question

Catching the last xpath element

Could someone please help with how can I get the last

xpath
element?

I'm currently using the code.

String cardNum = driver.findElement(By.xpath(".//div[@class='card-number']/p")).getText();


I understand the method
[last()]
can be used to capture the last element and its value.

Thanks,

Az.

Answer

try the following:

driver.findElement(By.xpath("(//div[@class='card-number']/p)[last()]")).getText();
Comments