{"record":{"id":"85385cc8deb60c46","repo":"SeleniumHQ/selenium","slug":"the-specified-identifier-id-is-not-found-in-t","errorCode":null,"errorMessage":"The specified identifier '#{id}' is not found in the window handle list","messagePattern":"The specified identifier '#(.+?)' is not found in the window handle list","errorType":"exception","errorClass":"Error::NoSuchWindowError","httpStatus":null,"severity":"error","filePath":"rb/lib/selenium/webdriver/common/target_locator.rb","lineNumber":95,"sourceCode":"      # switch to the given window handle\n      #\n      # If given a block, this method will switch back to the original window after\n      # block execution.\n      #\n      # @param id\n      #   A window handle, obtained through Driver#window_handles\n      #\n\n      def window(id)\n        if block_given?\n          original = begin\n            @bridge.window_handle\n          rescue Error::NoSuchWindowError\n            nil\n          end\n\n          unless @bridge.window_handles.include? id\n            raise Error::NoSuchWindowError, \"The specified identifier '#{id}' is not found in the window handle list\"\n          end\n\n          @bridge.switch_to_window id\n\n          begin\n            yield\n          ensure\n            current_handles = @bridge.window_handles\n            original = current_handles.first unless current_handles.include? original\n            @bridge.switch_to_window original\n          end\n        else\n          @bridge.switch_to_window id\n        end\n      end\n\n      #\n      # get the active element","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/SeleniumHQ/selenium/blob/aa36b38e696a0909e973bdf5e2f9031ffe842c4b/rb/lib/selenium/webdriver/common/target_locator.rb#L77-L113","documentation":"Raised by TargetLocator#window when called with a block and the provided window handle (id) is not present in the current list of open window handles. This is a client-side pre-check before attempting the actual switch_to_window bridge call. The error is NoSuchWindowError, a standard Selenium error class.","triggerScenarios":"Calling driver.switch_to.window('stale-handle') { ... } where the handle was from a window that has since been closed. Using a handle stored in a variable across navigations that closed the original window. Race condition: the window was closed between obtaining the handle list and calling window(). Passing a fabricated or empty string as a handle.","commonSituations":"Storing a window handle early in a test, navigating through multiple page changes, then trying to switch back to a window that was closed. Multi-window test flows where tabs are closed programmatically. Concurrency or timing issues in CI where the browser state changes between handle capture and use.","solutions":["Always fetch the current handle list fresh before switching: driver.window_handles.","Store the original handle via driver.window_handle at the start and verify it's still in driver.window_handles before switching back.","Wrap window switching in a begin/rescue for Error::NoSuchWindowError and fall back to the first available handle.","Avoid hardcoding handles; they are opaque session-generated strings."],"exampleFix":"// before\noriginal = driver.window_handle\ndriver.switch_to.new_window(:tab)\n# ... tab gets closed by page action ...\ndriver.switch_to.window(original) { ... }  # original may be gone\n\n// after\noriginal = driver.window_handle\ndriver.switch_to.new_window(:tab)\n# ensure handle still exists before switching\ntarget = driver.window_handles.include?(original) ? original : driver.window_handles.first\ndriver.switch_to.window(target) { ... }","handlingStrategy":"validation","validationCode":"def safe_switch_window(driver, handle)\n  handles = driver.window_handles\n  target = handles.include?(handle) ? handle : handles.first\n  warn \"Window handle '#{handle}' not found; switching to first available: #{target}\" unless handles.include?(handle)\n  driver.switch_to.window(target)\nend","typeGuard":null,"tryCatchPattern":"begin\n  driver.switch_to.window(handle) { yield }\nrescue Selenium::WebDriver::Error::NoSuchWindowError => e\n  raise unless e.message.include?('not found in the window handle list')\n  # handle is stale; switch to first available\n  driver.switch_to.window(driver.window_handles.first)\nend","preventionTips":["Always fetch driver.window_handles fresh before switching, never use cached handle lists.","Store the original handle at the start of a multi-window flow and verify it still exists before returning.","Wrap window switching logic in a helper that checks handle validity and falls back gracefully.","Call driver.quit in ensure blocks so windows are closed deterministically, not by page actions."],"tags":["window","window-handle","stale-handle","ruby"],"backgroundTag":null,"analyzedSha":"aa36b38e696a0909e973bdf5e2f9031ffe842c4b","analyzedAt":"2026-08-14T02:32:32.244Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}