2

In Protractor, there is the .first() and .last() methods available on ElementArrayFinder:

var elements = element.all(by.css(".myclass"));

elements.last();
elements.first();

But, how to get the element just before the last one (penultimate) without knowing how many elements are there in total?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Ah, assumed you were using jQuery with the $, did not look at the tags. – epascarello Jun 21 '16 at 01:04
  • @epascarello ah, sorry for the confusion. Protractor has `$$` shortcut as well. Replaced the shortcut with the full expression to avoid confusion. Thanks. – alecxe Jun 21 '16 at 01:05

1 Answers1

6

You can use negative indexing to get the elements from the end by index:

Negative indices are wrapped (i.e. -i means ith element from last)

elements.get(-2);  // the element before last
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195