Capybara: Select an Option by Value Not Text

Capybara: Select an option by value not text

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

Capybara select_option is found, but not selected

Thanks.

I solved the problem. Sorry, it is a stupid solution.

I forgot in my Application Controller to include:

before_action :configure_permitted_parameters, if: :devise_controller?

https://github.com/plataformatec/devise#strong-parameters

How to select option in drop down using Capybara

If you take a look at the source of the select method, you can see that what it does when you pass a from key is essentially:

find(:select, from, options).find(:option, value, options).select_option

In other words, it finds the <select> you're interested in, then finds the <option> within that, then calls select_option on the <option> node.

You've already pretty much done the first two things, I'd just rearrange them. Then you can tack the select_option method on the end:

find('#organizationSelect').find(:xpath, 'option[2]').select_option

Capybara, selecting 1st option from dropdown?

You can find the first option element and then use the select_option method to select it.

For example, if the select list has an id "select_id", you can do:

first('#select_id option').select_option

As @TomWalpole mentions, this will not wait for the element to appear. It would be safer to do one of the following:

first('#select_id option', minimum: 1).select_option

or

find('#select_id option:first-of-type').select_option

How do I select an element from the grouped selection by its value?

This answer worked for me Capybara: Select an option by value not text by @d_rail

You create a helper first. I put this helper in spec/support/utilities.rb

def select_by_value(id, value)
option_xpath = "//*[@id='#{id}']/option[@value='#{value}']"
option = find(:xpath, option_xpath).text
select(option, :from => id)
end

Then to use it:

select_by_value "select_id", "select_option"

In my case, the select tag has the id user_category and the option I wanted to select was Musician. So my example was

select_by_value "user_category", "Musician"

Get select value of dropdown for capybara testing

find_field('restrictions__rating_movies').find('option[selected]').text

Cannot select option in capybara

Capybaras #select only works with visible html option elements. The issue here is that you appear to be using a kendo multiselect widget which hides the real option elements ( btw your html is invalid since option elements are only legally allowed to be inside a select, optgroup, or datalist element) and then generates a visible UI using li elements inside a ul.k-list. From looking at a demo of the kendo multiselect - http://demos.telerik.com/kendo-ui/multiselect/index - it appears the actual ul.k-list element is attached to the page outside of the widget div and therefore a find can't be scoped to the widget, and the ul.list is given an id of "#{id of select hidden in multiselect widget}_list". Since your html doesn't appear to have an id on the hidden element in the widget there is no way to scope with that either. Because of all of that, rather than doing select(item) you should be able to use

find('.k-list li.k-item', text: item).click

to click on the element in the dropdown that becomes visible after you click in the header. It would be better if this find could be scoped to a particular .klist on the page, but hopefully there is only ever one actually visible while you're running the tests.



Related Topics



Leave a reply



Submit