selenium xpath, how to select last matching element in a table?

try with cssSelector, this way.

By.cssSelector("table tr:last-child td")

The answer was exactly where I put the [last()] and I had it in the wrong place

It goes here:

//div[@id='content']//table//tr[last()]//td[contains(text(),'service')][last()]/following-sibling::td[2]

Not here:

//div[@id='content']//table//tr[last()]//td[contains(text(),'service')]/following-sibling::td[2][last()]

List<WebElement> allElement=fd.findElements(By.xpath("//table//td[contains(.,'service')]");
int count=allElement.size();
allElement.get(count-1).click();

Tags:

Selenium