我怎样才能让我的脚本循环?

How can I get my script to loop?

我在我的脚本登录并进入浏览器的地方有它 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。 感谢您的帮助。

您似乎没有正确调用 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

至少会让引擎盖下发生的一切变得更加清晰。