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 回答