【问题标题】:Ruby : Watir : How to avoid closing browser from Net::ReadTimeout?Ruby : Watir : 如何避免从 Net::ReadTimeout 关闭浏览器?
【发布时间】:2020-05-01 00:28:31
【问题描述】:

我正在使用 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

【问题讨论】:

    标签: ruby while-loop webdriver watir


    【解决方案1】:

    您必须增加页面加载超时,它等待默认时间为 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。

    【讨论】:

    • 您好,我已经使用了这种方法,但是有些页面需要很长时间才能加载,并且如果它是任何方法(例如等待特定时间然后移动到下一个迭代),仍然会出现错误,如我发布的例子
    • 此代码自动等待页面加载作为点击的结果。我把时间从60增加到120,如果还不够,你还可以增加。您不必在代码中重试。设置完成后,您的 b.element.click 会自动等待直到下一页加载。
    • 如果页面在特定时间没有加载,有什么办法可以做到,然后关闭浏览器并像我的示例那样转到下一行
    【解决方案2】:

    这是一个老问题,但也许对某人有帮助:

    client = Selenium::WebDriver::Remote::Http::Default.new
    client.timeout = 600 # instead of the default 60 (seconds)
    Watir::Browser.new :chrome, http_client: client
    

    【讨论】:

    • 已弃用。使用 client.read_timeout。
    猜你喜欢
    • 1970-01-01
    • 2019-08-28
    • 2016-08-09
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多