Sig
Sig

Reputation: 5948

Find select knowing just part of its name

In Capybara, how can I refer to a select knowing only part of its name?

I have the following piece of HTML

<select name="user[work_hours][HASH][end_hour]">
 ...
</select>

Where HASH is an unknown value, and I need to find the select. Can I use a regular expression?

Upvotes: 0

Views: 34

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49890

There are many ways to do what you want. Depending on surrounding HTML you may be able to scope the find to a section of the page that only includes the one select so you don't need to specify anything to match the select at all

find('css to locate wrapping/scoping element').find(:select)

If that's not possible you can use CSS attribute begins-with and ends-with selectors like

find('select[name^="user[work_hours]["][name$="][end_hour]"]')

If that seems too complicated you can use the :element selector type which will allow you to match on any attribute with a string or regex. This may be inefficient on large pages with many select elements.

find(:element, :select, name: /user\[work_hours\]\[.*\]\[end_hour\]/

Another solution if the options in the select are unique on the page would just be to select the option and not worry about finding the actual select element

select('text of option')

Upvotes: 1

Related Questions