如何限制Watir收集的按钮数量?

时间:2018-04-05 10:10:49

标签: ruby selenium-webdriver twitter watir

我正在收集推特的关键按钮。如何将按钮限制为仅30个按钮而不是页面上的所有内容?

    #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

2 个答案:

答案 0 :(得分:3)

元素集合包括Enumerable,因此您可以使用#take

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

答案 1 :(得分:1)

使用#shift方法

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

  b.click

end