Search code examples
rubycapybara

Capybara: Select an option by value not text


For the HTML

<select id="date">
  <option value="20120904">Tue 4 Sep 2012</option>
  <option value="20120905">Wed 5 Sep 2012</option>
  <option value="20120906">Thu 6 Sep 2012</option>
</select>

I have the following Capybara Ruby code:

select "20120905", :from => "date"

But this errors with:

cannot select option, no option with text '20120905' in select box 'date' (Capybara::ElementNotFound)

However, if I do

select "Wed 5 Sep 2012", :from => "date"

It's ok.

Is it possible to select an option in Capybara by Value not Text?

Thanks


Solution

  • This will work to select an option by value:

    find("option[value='20120905']").click
    

    To maintain the scope of the selector you could wrap it in a within block as such:

    within '#date' do
      find("option[value='20120905']").click
    end