user411103
user411103

Reputation:

Ruby/Watir: go to next button of the same class

I have a table where all the button are like this:

<button class="button_class btn" type="button">

I want to click in all of them using Watir/Ruby. This is the code to click on the first one:

browser.button(:class => 'button_class btn').click

What can I do to go to the next one and click on it?

Thanks

Upvotes: 2

Views: 1070

Answers (2)

Justin Ko
Justin Ko

Reputation: 46846

If you are looking to just click the second button (rather than clicking each and every button), you can use the :index locator.

browser.button(:class => 'button_class btn', :index => 1).click

Note:

  • The :index is the position of the elements matched - ie :index 1 is the second button that has the class 'button_class btn'.
  • The :index is 0-based - ie 0 is the first matching element, 1 is the second matching element, etc.

Upvotes: 2

Arup Rakshit
Arup Rakshit

Reputation: 118289

I think you need to use buttons instead of button as below :-

browser.buttons(:class => 'button_class btn').each do |b|
  b.click
end

Upvotes: 2

Related Questions