find_field with exact label text

33 views
Skip to first unread message

Franz Liedke

unread,
May 8, 2023, 6:02:57 AMMay 8
to Capybara
Hello and thanks for Capybara!

I am having trouble using the find_field query to match fields by exact label text.

FORM
<label>Word1 Word2 <input /></label>
<label>Word1 <input /></label>

CODE
find_field("Word1")

I want this to match the second input.
Without further options, I get a Capybara::Ambiguous error (makes sense).
With {exact: true}, no element is found.
With {exact_text: true}, I also get the ambiguous match.

What am I doing wrong? Is there some valid combination of options that should work with #find_field?

Regards
Franz

Thomas Walpole

unread,
May 10, 2023, 12:56:57 AMMay 10
to Capybara
`exact` affects the `text` option which can be passed to finders, and `exact_text: 'some string'` is a shortcut for `text: 'some string', exact: true`.  What you're trying to do is adjust the matching method used for the locator passed to the finder.  How to do that depends on what matching mode you have set as the default.  Assuming you haven't changed the default from :smart, then `find_field("Word1 ")` (assuming the space you show in your HTML is really there) should find the field you want since it will default to finding the exact label match.
Reply all
Reply to author
Forward
0 new messages