Method: Capybara::Node::Actions#select

Defined in:
lib/capybara/node/actions.rb

#select(value = nil, from: nil, **options) ⇒ Capybara::Node::Element

If from option is present, #select finds a select box, or text input with associated datalist, on the page and selects a particular option from it. Otherwise it finds an option inside current scope and selects it. If the select box is a multiple select, #select can be called multiple times to select more than one option. The select box can be found via its name, id, test_id attribute, or label text. The option can be found by its text.

page.select 'March', from: 'Month'

If the driver is capable of executing JavaScript, this method will wait for a set amount of time and continuously retry finding the element until either the element is found or the time expires. The length of time this method will wait is controlled through default_max_wait_time.

Parameters:

  • value (String) (defaults to: nil)

    Which option to select

  • from (String) (defaults to: nil)

    The id, test_id attribute, name or label of the select box

Options Hash (**options):

  • wait (false, true, Numeric)

    Maximum time to wait for matching element to appear. Defaults to default_max_wait_time.

Returns:

Raises:

  • (ArgumentError)

200
201
202
203
204
205
206
207
208
209
210
# File 'lib/capybara/node/actions.rb', line 200

def select(value = nil, from: nil, **options)
  raise ArgumentError, 'The :from option does not take an element' if from.is_a? Capybara::Node::Element

  el = from ? find_select_or_datalist_input(from, options) : self

  if el.respond_to?(:tag_name) && (el.tag_name == 'input')
    select_datalist_option(el, value)
  else
    el.find(:option, value, **options).select_option
  end
end