Capybara: Select an option by value not text

2019-01-31 10:21发布

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

标签: ruby capybara
8条回答
Deceive 欺骗
2楼-- · 2019-01-31 11:10

Click using find_field works fine:

find_field("date").find("option[value='20120905']").click
查看更多
女痞
3楼-- · 2019-01-31 11:10

You could also use capybara-ui which will look first to match the text, then to match the value.

# define your form widget, in this case in a role
class UserRole < Capybara::UI::Role
  form :my_form do
    select :my_select, 'my_select'
  end
end

# then just submit your form params via #submit
role = UserRole.new

role.submit :my_form, my_select: '20120905'

See more about capybara-ui forms here.

查看更多
登录 后发表回答