How to find last matching element in capybara?

Sandip Ransing picture Sandip Ransing · Aug 29, 2013 · Viewed 15.9k times · Source

I wanted to do find by class_name and return last element among all elements.

In jquery, we can write something like below but that don't seems to work in capybara

find(".delete:last")

If you are using webkit driver you can write something like -

find(".delete:last-child")

In capybara, you can get last element using below -

all(:css, ".delete").last

Is there any alternative to above?

Answer

Sharagoz picture Sharagoz · Jul 4, 2014

I had the same problem and ended up using this method in Capybara 2.4

all('.delete').last