【问题标题】:How to limit the amount of buttons Watir collects?如何限制 Watir 收集的按钮数量?
【发布时间】:2018-09-15 02:54:37
【问题描述】:

我正在收集 Twitter 的关注按钮。如何将按钮限制为仅 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

【问题讨论】:

    标签: ruby selenium-webdriver twitter watir


    【解决方案1】:

    元素集合包含Enumerable,所以可以使用#take:

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

    【讨论】:

      【解决方案2】:

      使用#shift方法

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-11
        • 2017-05-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-23
        相关资源
        最近更新 更多