title: Automate timeout handling (wait and refresh) in watir
body: <p>I have always learned that good coding means: do not repeat yourself. But these days I keep repeating myself, in an attempt to let my scrapers handle timeout errors.</p>
  
  <p>For every link or button that I click, i add a rescue Exception => e and refresh the page. </p>
  
  <p>For example </p>
  
  <pre><code>browser.link(:xpath, "//tr[@class='pager'][1]/td/a").when_present.click
  </code></pre>
  
  <p>turns into</p>
  
  <pre><code>begin
    browser.link(:xpath, "//tr[@class='pager'][1]/td/a").wait_until_present
    browser.link(:xpath, "//tr[@class='pager'][1]/td/a").click
  rescue Exception =&gt; e
    sleep (10)
    puts "timed out, let's refresh"
    browser.refresh
  end
  </code></pre>
  
  <p>Perhaps there is a cleaner, non-repetitive solution. I've tried manipulating the Watir::wait but I could not get that to work..</p>
  
  <pre><code>class Watir::Wait
    alias_method :__do_wait, :wait_until
    def wait_until
      begin
        __do_wait{ yield }
      rescue Exception =&gt; e
        puts "timed out. let's refresh"
        $browser.refresh
      end
    end
  end
  </code></pre>