I am using the Cocoon gem to build a nested form in Rails. This gem assigns a random number to each duplicated form element in order to distinguish between them.
For example:
id="challenge_events_attributes_1464333427019_event_time_3i"
Where '1464333427019' is a random number.
I have tried various iterations of this:
x = page.all(:xpath, '//input[contains("challenges_events_attributes")]')
puts "X: #{x.inspect}"
Assuming I have multiple elements on a page, how can I target these elements with Capybara (perhaps using xpath), and then assign values to them?
There is no need to use xpath, the CSS attribute starts with selector will work fine for this
page.all('input[id^="challenges_events_attributes_"]').each do |el|
el.set('whatever value you want to set')
end
if you need it to match the end of the id too you can combine with the attribute ends with selector page.all('input[id^="challenges_events_attributes_"][id$="_event_time_3i"]')
etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With