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

Web DevProductivity

Keeping on top of website updates

Learn what website updates are, what they entail, why they are important, and how we can help

Freelancing

Getting the most out of your agency

Here are some tips based on years of working with, for, and as an agency on how to get the most out of any work you do.