Search code examples
rubyselenium-webdrivertwitterwatir

How to limit the amount of buttons Watir collects?


I'm collecting the follow buttons of twitter. How can I limit the buttons to just 30 buttons not everything on the page ?

    #Collect the "Follow" buttons
    browser.spans(:class => ['user-actions-follow-button js-follow-btn follow-button']).each do |b|

        #Click them! One by one.
        b.click

        # Generate random sleep period
        r = Random.rand(4...7)

        #Sleep so not to appear like a bot.
        sleep(r)
        # end
    end

Solution

  • Use #shift method

    browser.spans(:class => ['user-actions-follow-button js-follow-btn follow-button'])
        .to_a
        .shift(30)
        .each do |b|
    
      b.click
    
    end