{"record":{"id":"0e4a3dfa227d261b","repo":"teamcapybara/capybara","slug":"window-cannot-be-switched-inside-a-within-block","errorCode":null,"errorMessage":"Window cannot be switched inside a `within` block","messagePattern":"Window cannot be switched inside a `within` block","errorType":"exception","errorClass":"Capybara::ScopeError","httpStatus":null,"severity":"error","filePath":"lib/capybara/session.rb","lineNumber":920,"sourceCode":"    def _find_frame(*args, **kw_args)\n      case args[0]\n      when Capybara::Node::Element\n        args[0]\n      when String, nil\n        find(:frame, *args, **kw_args)\n      when Symbol\n        find(*args, **kw_args)\n      when Integer\n        idx = args[0]\n        all(:frame, minimum: idx + 1)[idx]\n      else\n        raise TypeError\n      end\n    end\n\n    def _switch_to_window(window = nil, **options, &window_locator)\n      raise Capybara::ScopeError, 'Window cannot be switched inside a `within_frame` block' if scopes.include?(:frame)\n      raise Capybara::ScopeError, 'Window cannot be switched inside a `within` block' unless scopes.last.nil?\n\n      if window\n        driver.switch_to_window(window.handle)\n        window\n      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","sourceCodeStart":902,"sourceCodeEnd":938,"githubUrl":"https://github.com/teamcapybara/capybara/blob/15b5fdb76e972e9623d5af2123ed3755594f9732/lib/capybara/session.rb#L902-L938","documentation":"The second guard in _switch_to_window: it also refuses to switch when an element scope is innermost (scopes.last non-nil), i.e. a within block is active. It is the internal backstop for the public switch_to_window check (session.rb:517) and covers within_window and other internal call paths, so hitting this line means a within scope was live while the internal switcher ran.","triggerScenarios":"Calling the private page.send(:_switch_to_window, w) from helper code inside a within block; monkey-patches or debugging snippets that bypass the public API's own ScopeError.","commonSituations":"Helper libraries invoking the private switcher directly; monkey-patched session code that reaches the internal method with an element scope still on the stack.","solutions":["Use the public API and move the switch outside any within block","Use within_window(w) { ... } for window-scoped work instead of the internal switcher","Remove monkey-patches/helpers that call page.send(:_switch_to_window, ...)"],"exampleFix":"# before\nwithin('.results') do\n  page.send(:_switch_to_window, w) # Capybara::ScopeError\nend\n\n# after\nswitch_to_window(w)\nwithin('.results') { click_link 'First' }","handlingStrategy":"validation","validationCode":"page.switch_to_window(w) if page.send(:scopes).last.nil? && !page.send(:scopes).include?(:frame)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never call private session internals; use switch_to_window/within_window","Keep window switches out of within blocks"],"tags":["windows","scope","within","internal-api"],"backgroundTag":"window-scope-violation","analyzedSha":"15b5fdb76e972e9623d5af2123ed3755594f9732","analyzedAt":"2026-08-21T16:53:45.588Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}