browsingContext.close command

The browsingContext.close command of the browsingContext module closes the specified top-level context.

Syntax

json
{
  "method": "browsingContext.close",
  "params": {
    "context": "<contextId>"
  }
}

Parameters

The params field contains:

context

A string that contains the ID of the top-level context to close. Context IDs are returned by commands such as browsingContext.getTree.

promptUnload Optional

A boolean that indicates whether the browser runs beforeunload event handlers before closing the context. The default value is false.

  • false: The specified context closes immediately without running beforeunload event handlers.
  • true: The browser runs beforeunload event handlers before closing the specified context. Any resulting prompt is handled as defined by the unhandledPromptBehavior capability specified via the session.new command.

Return value

The result field in the response is an empty object ({}).

Errors

invalid argument

A required parameter is missing or has an invalid type. This error is also returned when the context specified by context is not a top-level context.

no such frame

No context with the given context ID is found.

Examples

Closing a tab with a page unload prompt

The following example shows how to close a tab and allow its beforeunload event handlers to run before closing.

With a WebDriver BiDi connection, suppose a session is created via session.new with unhandledPromptBehavior set to "accept". First get the context ID using browsingContext.getTree, then send the following message:

json
{
  "id": 1,
  "method": "browsingContext.close",
  "params": {
    "context": "5e5e96e8-5247-4f22-9b35-a4a2d841cbaa",
    "promptUnload": true
  }
}

The browser closes the context and responds as follows:

json
{
  "id": 1,
  "type": "success",
  "result": {}
}

Since promptUnload is true, the browser runs any beforeunload handlers on the page before closing. The confirmation prompt, if shown, is automatically accepted based on the unhandledPromptBehavior setting defined in session.new.

Specifications

Specification
WebDriver BiDi
# command-browsingContext-close

Browser compatibility

See also