Capybara: Capybara isn't finding select options

Created on 8 Feb 2014  路  10Comments  路  Source: teamcapybara/capybara

I'm using Capybara 2.2.1 with Rspec.

I have this code on my page:

<label for="question_category_id">Category</label>
<select id="question_category_id" name="question[category_id]">
  <option value="1">General</option>
</select>

And I've tried each of these:

select "General", from: "question_category_id"
select "General", from: "question[category_id]"
select "General", from: "Category"
page.select "General", from: "Category"

I get the same error every time:

Failure/Error: select "General", from: "Category"
Capybara::ElementNotFound:
  Unable to find option "General"

Everything I've read says that all of these should work. Am I doing something wrong?

Most helpful comment

select X, from: Y

X should be an option label, not an option value

All 10 comments

I would guess that your option elements are being hidden through css or something along those lines. You can verify this by doing

select "General", from: "Category", visible: false

Also this question is better asked on the mailing list

One other possibility is you have multiple selects and have set Capybara.match = :first - although I don't think thats it since you have tried every different locator format for it, and you would have had to have multiple selects with the same id to keep seeing the issue

Thanks for the response. The select is definitely visible, but I tried visible: false just as a sanity check -- no dice. This is also the only select in the form, so that can't be it either.

I'll ask on the mailing list. Thanks.

I have the exact same problem with 2.2.1, and it was working perfectly before with 2.0.2. All my specs with a select failed now. @kiddrew where you able to fix that on our side?

I did get it working, but I can't remember at this point what I did...

select X, from: Y

X should be an option label, not an option value

That also happens when using Materializecss (with js: true), in that case is enough to pass the visible: false option (thanks @twalpole)

I just spent an afternoon on this issue, when doing a bit of debugging noticed that some of the fields were empty on the form, which led me to clone the development DB.

Rake db:test:prepare

I was facing the same issue. It turned out my factories were the problem. They weren't being created at all, hence the lack of the data to populate the <option> </option>. The following code helped troubleshoot them factories (placed in the spec files):

FactoryBot.factories.map(&:name).each do |factory_name|
    describe "The #{factory_name} factory" do
        it "is valid" do 
            factory_thing = build(factory_name)
            expect(factory_thing).to be_valid
        end
    end
end 

@AngeloRwanda FYI -- You don't need to write that code yourself, FactoryBot provides it via the lint method - https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#linting-factories

Was this page helpful?
0 / 5 - 0 ratings