rubyelementwatir-webdriver

Watir-Webdriver: I want to click on the last button with the same class or value


I am having trouble having my script work and finding a suitable solution.

The issue I am facing is I cannot get my code to go to the last button on the page and click on it. It seems like a trivial issue, but I am well and truly stumped. Can someone point me in the right direction?

Here is my code of what I have tried albeit with no success

@browser.button.last(:value => 'Unlock').click or @browser.button(:class => 'btn btn-danger').last.click

The error I keep getting is

undefined method 'last'

The reason why I cannot use an index in my code is because the number of buttons on the web page are not constant as the number of buttons on display can randomly increase or decrease. Hence my predicament.


Solution

  • You could use the buttons method to map all the buttons on the page and then click use the last method on the collection. For example:

    buttons = b.buttons(:class => "class_name_here")
    buttons.last.click