# Newbie ruby watir exception handling

**URL:** <https://rubytalk.org/t/newbie-ruby-watir-exception-handling/58186>\
**Category:** ruby-talk\
**Created:** [16 April 2010 15:38 UTC](https://rubytalk.org/t/newbie-ruby-watir-exception-handling/58186 "2010-04-16T15:38:25Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Esteban\_Chan](https://avatars.discourse-cdn.com/v4/letter/e/e56c9b/32.png) [@Esteban\_Chan](https://rubytalk.org/u/Esteban_Chan)\
**Post date:** [16 April 2010 15:38 UTC](https://rubytalk.org/t/newbie-ruby-watir-exception-handling/58186/1 "2010-04-16T15:38:25Z")

</div>

We're testing. This script below will go through the list of url's, and  
on each url, write out the results of the script to a file. These  
results are a) the last 3 digits of the url string  
b) the 1st href on the page  
c) the 3rd href on the page  
a,b,c are seperated by a tab. each url result array is on a separate  
line.

We run the following ruby-watir test script  
require 'rubygems'  
&nbsp;&nbsp;require 'hpricot'  
&nbsp;&nbsp;require 'watir'  
&nbsp;&nbsp;browser = Watir::Browser.new  
[  
('[http://www.example.com/add1.htm?prod=123'),('http://www.example.com/add1.htm?prod=124'),('http://www.example.com/add1.htm?prod=125](http://www.example.com/add1.htm?prod=123'),('http://www.example.com/add1.htm?prod=124'),('http://www.example.com/add1.htm?prod=125))].each  
{|url|  
browser.goto url  
sleep 4  
browser.link(:index, 1).href  
browser.link(:index, 3).href  
target=url.slice(-3..-1)+"\t"+browser.link(:index,  
1).href+"\t"+browser.link(:index, 3).href  
outfile = File.new("test.txt", "a")  
outfile.puts target  
}  
browser.close

> **···**
>
> ################################  
> if the javascript on the targeted url page loads slowly, or not at all,  
> ruby throws an exception  
> Watir::Exception::UnknownObjectException, since the 1st or 3rd link  
> aren't there.
> 
> we used "sleep 4" to slow down the script, but would rather have the  
> test run as fast as it can.
> 
> since I am a newbie to ruby, my exception handling block code attempts  
> aren't working - so my syntax is off. I've gone through more than a few  
> hours of trying various ways of handling the exception with 'rescue  
> Exception =\> e'
> 
> The exception handling should simply put an "unknown" as the values for  
> the missing links, and then go to the next url in the array.
> 
> This is our first ruby attempt. On another note, we landed on  
> ruby/watir/hpricot because we didn't locate any testing tools that will  
> run in FreeBSD's command line. This test script requires the browser to  
> process the javascript on the targeted url.
> 
> Does anyone know of a nix solution to testing instead?  
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
