Search code examples
rspectddcapybaracapybara-webkitspecifications

How to find first empty field in Capybara?


I want to find first empty field in form.

find("input[name='somename']", text: '').first

raise an error

Capybara::Ambiguous: Ambiguous match, found 5 elements matching css "input[name='somename']" with text ""


Solution

  • As of Capybara 2.0, find requires that only one element matches the specification. Per http://techblog.fundinggates.com/blog/2012/08/capybara-2-0-upgrade-guide/, you can use first with the same specification, as in:

    first("input[name='somename']", text: '')
    

    Although it would obviously be less efficient, you can also use:

    all("input[name='somename']", text: '')[0]