简体   繁体   中英

Protractor - Can I use $$(elementLocator).first() for more elements like the second, third and etc..?

Can I use $$(elementLocator).first() for more elements like the second, third and etc..?

or is it just for the first and the last elements?

For example - I've a drop down list with 5 buttons inside but they have the some properties and I want to click on all of them one by one.. so the first one will be:

$$("button.font-sm.md-button.md-ink-ripple").first().click();

And how can I click on the second option?

This is the element properties:

<button class="font-sm md-button md-ink-ripple" type="button" ng-transclude="" ng-click="actionClick(saction, $event)"><span class="ng-binding ng-scope">Message</span></button>

I know that it possible to use the Xpath, but i prefer some a stable locator.

You can use get() function that's built in protractor. get() function is a zero index based function. Here's how to use it -

$$("button.font-sm.md-button.md-ink-ripple").get(0).click(); //Click first element
$$("button.font-sm.md-button.md-ink-ripple").get(1).click(); //Click second element

You can always use first() and last() functions that are implementations of get() function internally. Hope it helps.

You can use .get(x), where x is the zero-based index of an item.

$$("button.font-sm.md-button.md-ink-ripple").get(1).click(); //Click second item
$$("button.font-sm.md-button.md-ink-ripple").last().click(); //Click last item

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM