4

I've done a search and most of the related google results have returned just in general selecting an element from a dropdown. However the ID's in this case for the elements in the dropdown are dynamically generated unfortunately.

This is for a base test case, so I basically just need to select for example the first one. The text is also the same for the elements in the dropdown (not sure if that helps).

Is there such an example of this?

Im using cucumber with caybara(using selenium driver) integration

msmith1114
  • 2,717
  • 3
  • 33
  • 84

4 Answers4

10

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
Justin Ko
  • 46,526
  • 5
  • 91
  • 101
  • The thing is the ID's for the options in the dropdown are dynamically generated...so I won't know what ID to use. The dropdown itself has a normal static ID if thats what you mean however? – msmith1114 Oct 01 '15 at 15:47
  • 1
    Yes, the "select_id" is the id of the select list itself (not its options). The CSS-selector `#select_id option` says find an element with id "select_id" and then within that element find the first option element. – Justin Ko Oct 01 '15 at 16:13
  • 2
    note - by default first won't wait for the element to appear on the page -- you may want to do first('#select_id option', minimum: 1).select_option if this is the first action after visiting a page – Thomas Walpole Oct 01 '15 at 19:35
  • @TomWalpole, thanks for the tip. I have incorporated it into the answer. – Justin Ko Oct 01 '15 at 20:16
  • What ended up working was this btw: select 'X-test', from: "dropdown-id" Im not sure why, but I could not get any of the above to work. I think maybe due to some custom dropdowns we had or something weirdly. – msmith1114 Oct 05 '15 at 19:29
3

Alternatively you can get the first element text then select it by select function:

first_element = find("#id_of_dropdown > option:nth-child(1)").text
select(first_element, :from => "id_of_dropdown")
Mesut GUNES
  • 7,089
  • 2
  • 32
  • 49
0

After two days of searching and reading, this article was amongst one of a few that was helpful. Hopefully, this can help someone else!

I created a few methods like so, excuse the naming..I changed it.

def some_dropdown(id, text)
  dropdown = find(id).click
  dropdown.first('option', text: text).select_option
end

def select_form
  within 'content#id' do
    some_dropdown('#id', text)

    click_link_or_button 'Submit'
  end
end

I also referenced this.

jasmineq
  • 521
  • 5
  • 7
0

I've tried to select an option from a modal dropdown. After trying all listed methods, and many other from other threads - I totally gave up and instead of using clicks or select_option just used keyboard keys

find(:select, "funding").send_keys :enter, :down, :enter

In case it still complains - try:

find(:select, "funding", visible: false).send_keys :enter, :down, :enter

Worked like a charm, selecting first option from a dropdown.

LE-HU
  • 631
  • 5
  • 17