【问题标题】:How can I get my script to loop?如何让我的脚本循环播放?
【发布时间】:2015-10-01 06:50:58
【问题描述】:

我在我的脚本登录并转到浏览器 url 的地方有它,但是当它退出当前网页时,它只是坐在那里,不会重新启动循环。我怎样才能让循环实现它的完成并重新启动?

x = 0

while x <= 5

File.open("yahoo_accounts.txt") do |email|
    email.each do |item|
    email, password = item.chomp.split(',')
    emails << email 
    passwords << password
    emails.zip(passwords) { |name, pass| 
        browser = Watir::Browser.new :ff
        browser.goto "url"

    #logs in and does what its suppose to do with the name and pass

        }
    end
    x += 1
    next
end
end

脚本完成后,它就在网页上...我正试图让它重新开始... 您会认为它会获取每个名称,传递并返回到开始的 url。 感谢您的帮助。

【问题讨论】:

    标签: ruby while-loop watir-webdriver webautomation


    【解决方案1】:

    您似乎没有正确调用browser.close。在我的快速模拟测试中,如果我不这样做,我肯定会出现奇怪的行为。您还使用了非惯用的 Ruby 循环。试试这个:

    5.times do 
      File.open("yahoo_accounts.txt") do |email|
        email.each do |item|
          email, password = item.chomp.split(',')
          emails << email 
          passwords << password
          emails.zip(passwords) do |name, pass| 
            browser = Watir::Browser.new :ff
            browser.goto "url"
    
            #logs in and does what its suppose to do with the name and pass
    
            browser.close
          end
        end
      end
    end
    

    编辑:

    或者,如果您希望相同的 Watir::Browser 实例完成所有工作,请在主循环之外进行初始化和关闭。现在,您正在生成一个新的 Browser 实例,每次迭代 emails.zip,乘以 email.each 的每次迭代,乘以 while 循环的 5 次迭代。这很笨拙,可能会破坏您的预期结果。所以就这样吧:

    browser = Watir::Browser.new :ff
    5.times do
      ... loop code ...
    end
    browser.close
    

    至少会让幕后发生的事情更加清晰。

    【讨论】:

    • 当我在脚本中使用 browser.close 时,它​​会完全停止运行
    • 所以最后一部分而不是 browser.close 我只是注销了站点。
    • 看看我最新的编辑,你肯定会产生大量的实例,在我的机器上打开了大量的浏览器窗口,很快就失控了。看看有没有帮助。
    猜你喜欢
    • 2011-06-02
    • 1970-01-01
    • 2014-05-02
    • 1970-01-01
    • 2015-08-27
    • 2015-03-24
    • 1970-01-01
    • 1970-01-01
    • 2015-09-29
    相关资源
    最近更新 更多