diff --git a/lib/capybara/node/actions.rb b/lib/capybara/node/actions.rb index 7139f810..b1d5194f 100644 --- a/lib/capybara/node/actions.rb +++ b/lib/capybara/node/actions.rb @@ -363,7 +363,6 @@ module Capybara def _check_with_label(selector, checked, locator, allow_label_click: session_options.automatic_label_click, **options) options[:allow_self] = true if locator.nil? - synchronize(Capybara::Queries::BaseQuery.wait(options, session_options.default_max_wait_time)) do el = find(selector, locator, **options) el.set(checked) @@ -372,7 +371,7 @@ module Capybara begin el ||= find(selector, locator, **options.merge(visible: :all)) - el.session.find(:label, for: el, visible: true).click unless el.checked? == checked + el.session.find(:label, for: el, visible: true, match: :first).click unless el.checked? == checked rescue StandardError # swallow extra errors - raise original raise e end diff --git a/lib/capybara/spec/session/check_spec.rb b/lib/capybara/spec/session/check_spec.rb index a4fae40c..ff6361f8 100644 --- a/lib/capybara/spec/session/check_spec.rb +++ b/lib/capybara/spec/session/check_spec.rb @@ -229,6 +229,12 @@ Capybara::SpecHelper.spec '#check' do @session.click_button('awesome') expect(extract_results(@session)['cars']).to include('bugatti') end + + it 'should check via label if multiple labels' do + expect(@session).to have_field('multi_label_checkbox', checked: false, visible: :hidden) + @session.check('Label to click', allow_label_click: true) + expect(@session).to have_field('multi_label_checkbox', checked: true, visible: :hidden) + end end end end diff --git a/lib/capybara/spec/views/form.erb b/lib/capybara/spec/views/form.erb index 04a873b1..ccfa243d 100644 --- a/lib/capybara/spec/views/form.erb +++ b/lib/capybara/spec/views/form.erb @@ -690,3 +690,15 @@ New line after and before textarea tag

+ + + +
Something random that justifies the usage of a separate label
+