How to get the element before last?

2019-07-26 14:34发布

问题:

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?

回答1:

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