1

私はサイトのリストを調べ、Watir を使用して各サイトに移動し、各ページのソース コードで何かを探しています。ただし、約 20 または 30 のサイトの後、特定のページをロードするときにブラウザーがタイムアウトし、スクリプトが壊れて、次のエラーが発生します。

rbuf_fill: 実行期限切れ (Timeout::Error)

タイムアウトを検出し、中断したところからサイトのテストを再開する方法を実装しようとしていますが、問題があります。これは私のコードです:

ie = Watir::Browser.new :firefox, :profile => "default"
testsite_array = Array.new
y=0
File.open('topsites.txt').each do |line|
testsite_array[y] = line
y=y+1
end
total = testsite_array.length
count = 0
begin
    while count <= total
        site = testsite_array[count]
        ie.goto site
        if ie.html.include? 'teststring'
            puts site + ' yes'
        else
            puts site + ' no'
        end

rescue
retry
    count = count+1
    end
end
ie.close
4

1 に答える 1

3

ループは次のようになります。

#Use Ruby's method for iterating through the array
testsite_array.each do |site|
    attempt = 1
    begin
        ie.goto site
        if ie.html.include? 'teststring'
            puts site + ' yes'
        else
            puts site + ' no'
        end 
    rescue
        attempt += 1

        #Retry accessing the site or stop trying
        if attempt > MAX_ATTEMPTS
            puts site + ' site failed, moving on'
        else
            retry
        end
    end
end
于 2013-04-19T18:39:55.890 に答える