kmancusi
kmancusi

Reputation: 571

Watir webdriver; looping and clicking specific number of elements

I'm using watir webdriver and rspec, and I'm looking to create a loop that takes search results and clicks on a hardcoded number of elements (i.e., if there are 50 results, I want to click on the first 5)
This is what I have so far:

asset_card = search_modal.divs(:class, 'asset-card selectable')
asset_card.each do |assets|
    assets.click
end

This currently goes and clicks on all the results that come back--is there a simple way to click on the first 5 or some other hardcoded value?

Upvotes: 0

Views: 50

Answers (1)

Justin Ko
Justin Ko

Reputation: 46846

The object returned by the divs method is Enumerable, which provides a variety of methods for interacting with the collection.

To take the first 5, use the take method:

asset_card = search_modal.divs(:class, 'asset-card selectable')
asset_card.take(5).each do |assets|
  assets.click
end

Upvotes: 2

Related Questions