{"record":{"id":"daa5c22960284b61","repo":"teamcapybara/capybara","slug":"could-not-find-a-window-matching-block-lambda","errorCode":null,"errorMessage":"Could not find a window matching block/lambda","messagePattern":"Could not find a window matching block/lambda","errorType":"exception","errorClass":"Capybara::WindowError","httpStatus":null,"severity":"error","filePath":"lib/capybara/session.rb","lineNumber":943,"sourceCode":"      else\n        synchronize_windows(options) do\n          original_window_handle = driver.current_window_handle\n          begin\n            _switch_to_window_by_locator(&window_locator)\n          rescue StandardError\n            driver.switch_to_window(original_window_handle)\n            raise\n          end\n        end\n      end\n    end\n\n    def _switch_to_window_by_locator\n      driver.window_handles.each do |handle|\n        driver.switch_to_window handle\n        return Window.new(self, handle) if yield\n      end\n      raise Capybara::WindowError, 'Could not find a window matching block/lambda'\n    end\n\n    def synchronize_windows(options, &block)\n      wait_time = Capybara::Queries::BaseQuery.wait(options, config.default_max_wait_time)\n      document.synchronize(wait_time, errors: [Capybara::WindowError], &block)\n    end\n  end\nend\n","sourceCodeStart":925,"sourceCodeEnd":952,"githubUrl":"https://github.com/teamcapybara/capybara/blob/15b5fdb76e972e9623d5af2123ed3755594f9732/lib/capybara/session.rb#L925-L952","documentation":"After the locator block fails on every open window, _switch_to_window_by_locator raises Capybara::WindowError: 'Could not find a window matching block/lambda'. Because the scan runs inside synchronize_windows, the entire iteration retries until the wait budget expires - so this error means no window ever satisfied the predicate within the wait, not merely that it was slow.","triggerScenarios":"switch_to_window { title == 'Dashboard' } when no window has that exact title (SPA sets document.title asynchronously, typo, page still loading past the wait); matching on the wrong attribute (title vs url); the target window closed before the switch.","commonSituations":"SPA titles that render late; exact-title predicates breaking after rebranding or label changes; default_max_wait_time too low on CI; OAuth popups auto-closing before the predicate runs.","solutions":["Raise the wait: switch_to_window(wait: 5) { title == 'Dashboard' }","Fix the predicate - prefer a url substring (current_url.include?('/chat')) over exact title match","Debug what actually exists: page.windows.map { |w| [w.title, w.url] }.inspect","If the window may have been closed, guard on page.driver.window_handles.size first"],"exampleFix":"# before\nswitch_to_window { title == 'Chat' } # WindowError: no match\n\n# after\nswitch_to_window(wait: 10) { current_url.include?('/chat') }","handlingStrategy":"retry","validationCode":"target = page.windows.find { |w| w.url.include?('/chat') }\nswitch_to_window(target) if target","typeGuard":null,"tryCatchPattern":"begin\n  switch_to_window { title == 'Chat' }\nrescue Capybara::WindowError\n  raise \"no window matched; open windows: #{page.windows.map { |w| [w.title, w.url] }.inspect}\"\nend","preventionTips":["Match on url substrings rather than exact titles","Pass explicit wait: for slow windows","Log open window titles/urls on WindowError to make mismatches obvious"],"tags":["windows","locator","switch-to-window","wait","predicate"],"backgroundTag":"window-not-found","analyzedSha":"15b5fdb76e972e9623d5af2123ed3755594f9732","analyzedAt":"2026-08-21T16:53:45.588Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}