3

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
Astrit Shuli
  • 619
  • 4
  • 20

2 Answers2

0

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.

Rajagopalan
  • 5,465
  • 2
  • 11
  • 29
  • 1
    Hello, i have used this method to but some pages take to much time to load and still got the error if it is any method like wait for a specific time and then move to the next iteration as in example that i post it – Astrit Shuli Jan 14 '20 at 10:35
  • This code automatically waits for page load as a result of the click. I increased the time from 60 to 120. If it is not enough,then you can still increase. You don't have to retry in your code. Once it's set, then your `b.element.click` automatically waits until next page loads. – Rajagopalan Jan 14 '20 at 10:37
  • 1
    Is there any way to make it if the page does not load for a specific time then close the browser and go to the next line like in my example – Astrit Shuli Jan 14 '20 at 12:42
0

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