Skip to main content
Use BrowserContext to coordinate pages and browser state that is shared across them.

Quick start

pages()

Return every open page in the browser context.
Promise<Page[]>
The operation result.

newPage()

Create a new page in the browser context.
string
The initial URL for the new page. Omit it to create a blank page.
Promise<Page>
The operation result.

activePage()

Return the page that is currently active.
Promise<Page | undefined>
The operation result.

setActivePage()

Make a page the active page in the browser context.
Page
The page to make active.
Promise<void>
Resolves after the operation completes.

close()

Close the remote browser context.
Promise<void>
Resolves after the operation completes.

addInitScript()

Run a script before scripts on every newly created page.
InitScriptSource<Arg>
A function, JavaScript source string, or object with exactly one of path and content.
Arg
An optional JSON-serializable argument. Valid only when script is a function.
Promise<void>
Resolves after the operation completes.

setExtraHTTPHeaders()

Set additional HTTP headers for every page in the context.
Record<string, string>
HTTP header names and values.
Promise<void>
Resolves after the operation completes.

getDomainPolicy()

Return the context’s current domain policy.
Promise<DomainPolicy | null>
The operation result.
string[]
Domains that the context may access.
string[]
Domains that the context must reject.

setDomainPolicy()

Set or clear the context’s domain policy.
DomainPolicy | null
The policy to apply, or null/None to clear it.
string[]
Domains that the context may access.
string[]
Domains that the context must reject.
Promise<void>
Resolves after the operation completes.

cookies()

Return cookies, optionally filtered to one or more URLs.
string | string[]
A URL or collection of URLs used to filter cookies.
Promise<Cookie[]>
The operation result.
string
The cookie name.
string
The cookie value.
string
The cookie domain.
string
The cookie path.
number
The Unix expiration time in seconds, or -1 for a session cookie.
boolean
Whether the cookie is restricted to HTTP requests.
boolean
Whether the cookie requires HTTPS.
Cookie['sameSite']
The cookie SameSite policy: "Strict", "Lax", or "None".

addCookies()

Add cookies to the browser context.
CookieParam[]
The cookies to add. Each requires name, value, and either url or both domain and path. Do not combine url with domain or path.
string
The cookie domain.
number
-1 for a session cookie or a non-negative Unix timestamp in seconds.
boolean
Whether the cookie is restricted to HTTP requests.
string
The cookie name.
string
The cookie path.
CookieParam['sameSite']
The cookie SameSite policy: "Strict", "Lax", or "None". "None" requires a secure cookie.
boolean
Whether the cookie requires HTTPS.
string
The URL scope. Cannot be about:blank or a data: URL.
string
The cookie value.
Promise<void>
Resolves after the operation completes.

clearCookies()

Clear cookies that match optional name, domain, and path filters.
ClearCookieOptions
Options that configure this operation.
string | RegExp
A cookie domain or regular-expression filter.
string | RegExp
A cookie name or regular-expression filter.
string | RegExp
A cookie path or regular-expression filter.
Promise<void>
Resolves after the operation completes.