Using Dropdowns in Watir Webdriver

How you can go about using dropdowns using the Watir Webdriver

I had been having some issues getting a dropdown to select a value using the Watir Webdriver and I wanted to briefly write up what I was doing verses what I should have been doing.

In my specific case I wanted to use Watir to drive a date selection using the jQuery datepicker so I inspected the elements and found a class I could latch onto to grab the year dropdown.

select(:class, 'ui-datepicker-year').select('2006')

I thought this would work, but it was telling me that instead of a symbol it was expecting an array. I am guessing this is because out of context it doesn't know if I mean a select element or I want to select something.

The fix was to chain my select with a parent element, like so:

div(:class, 'ui-datepicker-title').select(:class, 'ui-datepicker-year').select('2006')

Now Watir knows that .select means a select box and not to select something.

If you are interested in finding out more I have written up a short blog post about controlling the jQuery datepicker in Watir.


Recent posts View all

Ruby

How to ignore Bullet in RSpec tests

Using Bullet during a test can pick up mistakes but also has false negatives; here is an easy way to ignore them

Ruby

How we used Nokogiri to fix a sizing issue in YouTube's oEmbed

At some point, one of our calls to YouTube's oEmbed endpoint was returning videos way too small; we fixed in in our Rails application by using Nokogiri