问题描述
我正在使用 Watir 制作一个自动化程序,它从文件 links.txt
中读取链接,然后在 chrome 浏览器上逐个打开.当打开浏览器及其加载时间需要很长时间时,它会向我显示 Net::ReadTimeout
.我已尝试营救,如果没有营救,请转到列表中的下一个链接.
I am making an automation program using Watir , that reads links from a file links.txt
and then open one by one on chrome browser. When it takes to much time to open then browser and its on loading time it shows me the Net::ReadTimeout
. I have tried to rescue and and if its not rescued go to the next link from the list.
我已经尝试过这个,但是当 max_retries = 3 时它再次显示错误.我想让浏览器等待特定的时间,然后如果它仍在加载,请关闭浏览器并转到列表中的下一个链接
I have tried this one but when max_retries = 3 it shows again the error. I want to make browser to wait for specific amount of time and then if it is still loading close the browser and go to the next link from the list
file='links.txt'
max_retries = 3
times_retried = 0
n = 0
begin
browser = Watir::Browser.new :chrome
Watir.default_timeout = 1000
rescue Net::ReadTimeout
browser.wait
retry
end
line = File.readlines(file).sample
while n <= 50 do
n+=1
begin
browser.goto "#{line}"
rescue Net::ReadTimeout => error
if times_retried < max_retries
times_retried += 1
puts "Failed to load page, retry #{times_retried}/#{max_retries}"
retry
else
puts "Exiting script. Timeout Loading Page"
exit(1)
end
end
break if n == 50
end
推荐答案
这是一个老问题,但也许对某人有所帮助:
This is an old question, but maybe that helps someone:
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 600 # instead of the default 60 (seconds)
Watir::Browser.new :chrome, http_client: client
这篇关于Ruby:Watir:如何避免从 Net::ReadTimeout 关闭浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!