-4

I want to extract the text in the paragraph encircled in the screenshot. But from the last instance of the class, because this text is dynamic. Therefore I want the last text in this class.

I am tried this

String Reply2= driver.findElement(By.xpath("//div[@class='chat-message-content clearfix']/last[]")).getText(); ][1]

enter image description here

supputuri
  • 13,644
  • 2
  • 21
  • 39

2 Answers2

0

you can't use last[]

use [last()]

//div[contains(@class,'chat-message-content')][last()]/p
0

You are almost there, just need couple of changes in your xpath.

 String xPath = "(//div[@class='chat-message-content clearfix']/p)[last()]"

//div[@class='chat-message-content clearfix']/p should get all the p items under chat-message-content clearfix classes. Then group them by using ( and ). Now get the last item from the group using [last()].

Get the text by using the below line.

 String Reply2= driver.findElement(By.xpath(xPath)).getText();
supputuri
  • 13,644
  • 2
  • 21
  • 39