Close Window

The Close Window command of the WebDriver API closes the current top-level browsing context (window or tab) and returns with the list of currently open WebWindows. If it is the last window that is being closed, the WebDriver session will implicitly be deleted. Subsequent commands after the session is ended will therefore cause invalid session ID errors.

Syntax

Method URI template
DELETE /session/{session id}/window

URL parameters

session id
Identifier of the session.

Errors

Invalid session ID
Session does not exist.
Unexpected alert open
A user prompt, such as window.alert, blocks execution of command until it is dealt with.

Example

Python:

from selenium import webdriver

session = webdriver.Firefox()
original_window = session.window_handle

new_window = session.execute_script("return window.open()")
session.switch_to.window(new_window)

session.close()
session.switch_to.window(original_window)

Specifications

Specification Status Comment
WebDriver
The definition of 'Close_Window' in that specification.
Candidate Recommendation Initial definition

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support No1 No2557 No1 No3
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support No1 No1 No2 No No1 No3 No4

1. To enable Chromium support, include {"goog:chromeOptions": {"w3c": true}} in the capabiliites when running the command.

2. The vendor-supported implementation for Edge is non-spec-conforming and incompatible.

3. The vendor-supported implementation for Safari is non spec-conforming and incompatible.

4. The vendor-supported implementation for Samsung Internet is non-spec-conforming and incompatible.

See also

Document Tags and Contributors

 Contributors to this page: ato
 Last updated by: ato,