Capybara: Select an option by value not text
Solution 1:
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
Solution 2:
With Poltergeist as driver I can't click on an option like suggested in some of the other options above, instead you can do the following:
page.find_by_id('date').find("option[value='20120905']").select_option
Solution 3:
I wrote a helper method:
def select_by_value(id, value)
option_xpath = "//*[@id='#{id}']/option[@value='#{value}']"
option = find(:xpath, option_xpath).text
select(option, :from => id)
end
Save in a .rb file in spec/support/
Example use:
before do
select_by_value 'some_field_id', 'value'
click_button 'Submit'
end