我正在使用 Watir 制作一个自动化程序,它从文件中读取链接links.txt
然后在chrome浏览器上一一打开。当打开浏览器需要很长时间及其加载时间时,它会向我显示Net::ReadTimeout
。我已尝试救援,如果未获救,请转到列表中的下一个链接。
我已经尝试过这个,但是当 max_retries = 3 时,它再次显示错误。我想让浏览器等待特定的时间,然后如果它仍在加载,请关闭浏览器并转到列表中的下一个链接
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
您必须增加页面加载超时,默认等待时间为 60 秒,但您可以通过以下代码增加页面加载超时
client = Selenium::WebDriver::Remote::Http::Default.new
client.read_timeout = 120 # seconds
driver = Selenium::WebDriver.for :chrome,http_client: client
b=Watir::Browser.new driver
现在,您的代码将等待 120 秒以等待 #click 引起的任何页面加载,并等待通过 goto 方法加载 url。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)