Ruby : Watir : How to avoid closing browser from Net::ReadTimeout?

淺唱寂寞╮ 提交于 2021-02-07 08:55:19

问题


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.

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

回答1:


You have to increase the page load time out, it waits default time of 60 seconds but you can increase the page load timeout by the following code

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

Now your code would wait for 120 seconds for any page load which has been caused by #click and also wait to load the url by goto method.




回答2:


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


来源:https://stackoverflow.com/questions/59731079/ruby-watir-how-to-avoid-closing-browser-from-netreadtimeout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!