2015-07-13 61 views
0

我有它在腳本登錄並進入瀏覽器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。 感謝您的幫助。

回答

0

看起來你可能沒有正確地打電話給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 

至少會使發生在罩下更清楚的事情。

+0

當我在我的腳本中使用browser.close時,它會阻止它完全運行 – marriedjane875

+0

所以最後一部分而不是browser.close我只是退出該網站。 – marriedjane875

+0

看到我最新的編輯,你肯定會產生大量的實例,這些實例在我的機器上打開了大量的瀏覽器窗口,並且快速地失控。看看是否有幫助。 – wmjbyatt