Capybara writing drop down's options texts into an array

capybara, ruby

Solution

The problem is that `all('#MainContent_dd')` returns all elements that have the id `MainContent_dd`. Assuming this is your dropdown and ids are unique, it is expected that the `periods.length` is 1 (ie `periods` is the select list).

What you want to do is get the `option` elements instead of the `select` element.

Assuming your html is:

<select id="MainContent_dd">
    <option>Option A</option>
    <option>Option B</option>
    <option>Option C</option>
</select>

Then you can do:

  periods = find('#MainContent_dd').all('option').collect(&:text)
  p periods.length
  #=> 3
  p periods
  #=> ["Option A", "Option B", "Option C"]

What this does is:

- `find('#MainContent_dd')` - Finds the select list that you want to get the options from

- `all('option')` - Gets all option elements within the select list

- `collect(&:text)` - Collects the text of each option and returns it as an array

Problem

I'd like to put a dropdown list's options into an array generically in capybara. After the process I'm expecting to have an arrray of strings, containing all dropdown options. I've tried the code below but the length of my array stays 1 regardless of what the option count is. ``` periods = Array.new() periods = all('#MainContent_dd') print periods.length ```

Original source