Google Instant with FireWatir

I’ve been playing around with FireWatir, which allows you to write ruby scripts that control a Firefox web browser. This has obvious white hat uses, such as automated testing for sites that use JavaScript. It also has black hat uses for things like automated account creation and article directory submitters.

The most basic example is searching Google for something, and the code looks like this:


ff = FireWatir::Firefox.new
ff.goto('http://www.google.com/ncr')
ff.text_field(:name => 'q').set('yoda')
ff.button(:name, 'btnG').click

The problem is Google instant – after FireWatir types the first character, Google instant executes some AJAX which interferes with FireWatir. I found I would only get the first character typed. If you’re not running Firefox in “porn mode” you can disable Instant and your settings will be saved in a cookie. If you have Firefox configured not to save cookies, this is a problem. Here’s a solution that is somewhat of a kludge, but it works:


ff = FireWatir::Firefox.new
ff.goto('http://www.google.com/ncr')
ff.text_field(:name => 'q').set('y')
sleep 0.3
ff.text_field(:name => 'q').set('yoda')
ff.button(:name, 'btnG').click

The difference here is we only set the first character in the text field, then pause the script giving Instant time to update. Then proceed with the rest of the code.

WORDPRESS