def switch_to_window(self, window, wait=None):
if len(self._scopes) > <NUM_LIT:1>:<EOL><INDENT>raise ScopeError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if isinstance(window, Window):<EOL><INDENT>self.driver.switch_to_window(window.handle)<EOL>return window<EOL><DEDENT>else:<EOL><INDENT>@self.document.synchronize(errors=(WindowError,), wait=wait)<EOL>def switch_and_get_matching_window():<EOL><INDENT>original_window_handle = self.driver.current_window_handle<EOL>try:<EOL><INDENT>for handle in self.driver.window_handles:<EOL><INDENT>self.driver.switch_to_window(handle)<EOL>result = window()<EOL>if result:<EOL><INDENT>return Window(self, handle)<EOL><DEDENT><DEDENT><DEDENT>except Exception:<EOL><INDENT>self.driver.switch_to_window(original_window_handle)<EOL>raise<EOL><DEDENT>self.driver.switch_to_window(original_window_handle)<EOL>raise WindowError("<STR_LIT>")<EOL><DEDENT>return switch_and_get_matching_window()<EOL><DEDENT>
If ``window`` is a lambda, it switches to the first window for which ``window`` returns a
value other than False or None. If a window that matches can't be found, the window will be
switched back and :exc:`WindowError` will be raised.

Args:
    window (Window | lambda): The window that should be switched to, or a filtering lambda.
    wait (int | float, optional): The number of seconds to wait to find the window.

Returns:
    Window: The new current window.

Raises:
    ScopeError: If this method is invoked inside :meth:`scope, :meth:`frame`, or
        :meth:`window`.
    WindowError: If no window matches the given lambda.