SeleniumHQ/selenium · error · Error::WebDriverError

Switch to desired window before changing its size

Error message

Switch to desired window before changing its size

What it means

Raised by Remote::Bridge#resize_window (Error::WebDriverError) when a window handle other than the default :current is passed. The W3C WebDriver set-window-rect command always operates on the current window, so Selenium requires the caller to switch to the target window first; passing a specific handle is no longer supported.

Source

Thrown at rb/lib/selenium/webdriver/remote/bridge.rb:240

        def refresh
          execute :refresh
        end

        #
        # window handling
        #

        def window_handles
          execute :get_window_handles
        end

        def window_handle
          execute :get_window_handle
        end

        def resize_window(width, height, handle = :current)
          raise Error::WebDriverError, 'Switch to desired window before changing its size' unless handle == :current

          set_window_rect(width: width, height: height)
        end

        def window_size(handle = :current)
          unless handle == :current
            raise Error::UnsupportedOperationError,
                  'Switch to desired window before getting its size'
          end

          data = execute :get_window_rect
          Dimension.new data['width'], data['height']
        end

        def minimize_window
          execute :minimize_window
        end

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Call driver.switch_to.window(target_handle) before resizing, then call resize without a handle.
  2. Prefer the high-level API driver.manage.window.resize_to(width, height) which operates on the current window.
  3. Remove any explicit handle argument from resize_window calls.

Example fix

// before
bridge.resize_window(800, 600, saved_handle)

// after
driver.switch_to.window(saved_handle)
driver.manage.window.resize_to(800, 600)
Defensive patterns

Strategy: validation

Validate before calling

# Always operate on the current window; switch first when needed
driver.switch_to.window(target_handle)
driver.manage.window.resize_to(width, height)

Prevention

When it happens

Trigger: Calling bridge.resize_window(width, height, some_handle) with an explicit handle instead of the default :current; legacy code that passed a saved window handle to resize a non-current window.

Common situations: Porting code from the legacy JSON-Wire Protocol where resizing a specific handle was allowed; managing multiple windows and trying to resize one without switching to it first.

Related errors


AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14). Data as JSON: /api/errors/50a97d2466aaf582. Report an issue: GitHub.