Working with windows and tabs
Windows and tabs
Get window handle
WebDriver does not make the distinction between windows and tabs. If your site opens a new tab or window, Selenium will let you work with it using a window handle. Each window has a unique identifier which remains persistent in a single session. You can get the window handle of the current window by using:
Switching windows or tabs
Clicking a link which opens in a new window will focus the new window or tab on screen, but WebDriver will not know which window the Operating System considers active. To work with the new window you will need to switch to it. For this, we fetch all window handles, and store them in an array. The array position fills in the order the window is launched. So first position will be default browser, and so on.
Closing a window or tab
When you are finished with a window or tab and it is not the last window or tab open in your browser, you should close it and switch back to the window you were using previously. Assuming you followed the code sample in the previous section you will have the previous window handle stored in a variable. Put this together and you will get:
Forgetting to switch back to another window handle after closing a window will leave WebDriver executing on the now closed page, and will trigger a No Such Window Exception. You must switch back to a valid window handle in order to continue execution.
Create new window (or) new tab and switch
Creates a new window (or) tab and will focus the new window or tab on screen. You don’t need to switch to work with the new window (or) tab. If you have more than two windows (or) tabs opened other than the new window, you can loop over both windows or tabs that WebDriver can see, and switch to the one which is not the original.
Note: This feature works with Selenium 4 and later versions.
Opens a new tab and switches to new tab:
Opens a new window and switches to new window:
Quitting the browser at the end of a session
When you are finished with the browser session you should call quit, instead of close:
- Quit will:
- Close all the windows and tabs associated with that WebDriver session
- Close the browser process
- Close the background driver process
- Notify Selenium Grid that the browser is no longer in use so it can be used by another session (if you are using Selenium Grid)
Failure to call quit will leave extra background processes and ports running on your machine which could cause you problems later.
Some test frameworks offer methods and annotations which you can hook into to tear down at the end of a test.
If not running WebDriver in a test context, you may consider using
try / finally
which is offered by most languages so that an exception
will still clean up the WebDriver session.
Python’s WebDriver now supports the python context manager,
which when using the with
keyword can automatically quit the driver at
the end of execution.
Window management
Screen resolution can impact how your web application renders, so WebDriver provides mechanisms for moving and resizing the browser window.
Get window size
Fetches the size of the browser window in pixels.
Set window size
Restores the window and sets the window size.
Get window position
Fetches the coordinates of the top left coordinate of the browser window.
Set window position
Moves the window to the chosen position.
Maximize window
Enlarges the window. For most operating systems, the window will fill the screen, without blocking the operating system’s own menus and toolbars.
Minimize window
Minimizes the window of current browsing context. The exact behavior of this command is specific to individual window managers.
Minimize Window typically hides the window in the system tray.
Note: This feature works with Selenium 4 and later versions.
Fullscreen window
Fills the entire screen, similar to pressing F11 in most browsers.
TakeScreenshot
Used to capture screenshot for current browsing context. The WebDriver endpoint screenshot returns screenshot which is encoded in Base64 format.
TakeElementScreenshot
Used to capture screenshot of an element for current browsing context. The WebDriver endpoint screenshot returns screenshot which is encoded in Base64 format.
Execute Script
Executes JavaScript code snippet in the current context of a selected frame or window.
Print Page
Prints the current page within the browser.
Note: This requires Chromium Browsers to be in headless mode