Skip to main content
Version: 24.38.0

BrowserContext 类

🌐 BrowserContext class

BrowserContext 表示 浏览器 中的单个用户上下文。

当启动一个浏览器时,它至少有一个默认的浏览器上下文。可以使用Browser.createBrowserContext()创建其他上下文。每个上下文都有独立的存储(cookies/localStorage/等)。

🌐 When a browser is launched, it has at least one default browser context. Others can be created using Browser.createBrowserContext(). Each context has isolated storage (cookies/localStorage/etc.)

BrowserContext 触发 各种事件,这些事件在 BrowserContextEvent 枚举中有记录。

如果一个页面打开另一个页面,例如使用window.open,弹出窗口将属于父页面的浏览器上下文

🌐 If a page opens another page, e.g. using window.open, the popup will belong to the parent page's browser context.

语法

🌐 Signature

export declare abstract class BrowserContext extends EventEmitter<BrowserContextEvents>

Extends: EventEmitter<BrowserContextEvents>

附注

🌐 Remarks

在 Chrome 中,所有非默认上下文都是隐身模式,如果在启动浏览器时提供 --incognito 参数,默认浏览器上下文 也可能是隐身模式。

🌐 In Chrome all non-default contexts are incognito, and default browser context might be incognito if you provide the --incognito argument when launching the browser.

这个类的构造函数被标记为内部。第三方代码不应直接调用构造函数或创建继承 BrowserContext 类的子类。

🌐 The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the BrowserContext class.

示例

🌐 Example

创建一个新的 浏览器上下文

🌐 Creating a new browser context:

// Create a new browser context
const context = await browser.createBrowserContext();
// Create a new page inside context.
const page = await context.newPage();
// ... do stuff with page ...
await page.goto('https://example.com');
// Dispose context once it's no longer needed.
await context.close();

属性

🌐 Properties

属性

🌐 Property

修饰符

🌐 Modifiers

类型

🌐 Type

描述

🌐 Description

closed

readonly

boolean

这个 浏览器上下文 是否已关闭。

🌐 Whether this browser context is closed.

id

readonly

字符串 | 未定义

🌐 string | undefined

浏览器上下文 的标识符。

🌐 Identifier for this browser context.

方法

🌐 Methods

方法

🌐 Method

修饰符

🌐 Modifiers

描述

🌐 Description

browser()

获取与此 浏览器上下文 关联的 浏览器

🌐 Gets the browser associated with this browser context.

clearPermissionOverrides()

清除此 浏览器上下文 的所有权限覆盖。

🌐 Clears all permission overrides for this browser context.

close()

关闭此 浏览器上下文 及所有关联的 页面

🌐 Closes this browser context and all associated pages.

附注

无法关闭默认浏览器上下文

🌐 The default browser context cannot be closed.

cookies()

获取浏览器上下文中的所有 cookies。

🌐 Gets all cookies in the browser context.

deleteCookie(cookies)

在此浏览器上下文中删除 Cookie。

🌐 Removes cookie in this browser context.

deleteMatchingCookies(filters)

在此浏览器上下文中删除与提供的过滤器匹配的 Cookie。

🌐 Deletes cookies matching the provided filters in this browser context.

newPage(options)

在此浏览器上下文中创建一个新的页面

🌐 Creates a new page in this browser context.

overridePermissions(origin, permissions)

deprecated

在指定的 origin 内授予此 浏览器上下文 给定的 permissions

🌐 Grants this browser context the given permissions within the given origin.

已弃用:

支持 BrowserContext.setPermission()

🌐 in favor of BrowserContext.setPermission().

pages(includeAll)

获取此浏览器上下文中所有打开的页面的列表。

🌐 Gets a list of all open pages inside this browser context.

附注

非可见的页面,例如 "background_page",将不会在此列出。你可以使用Target.page()找到它们。

🌐 Non-visible pages, such as "background_page", will not be listed here. You can find them using Target.page().

setCookie(cookies)

在浏览器上下文中设置 cookie。

🌐 Sets a cookie in the browser context.

setPermission(origin, permissions)

为特定来源设置权限。

🌐 Sets the permission for a specific origin.

targets()

获取此 浏览器上下文 内的所有活跃 目标

🌐 Gets all active targets inside this browser context.

waitForTarget(predicate, options)

等待直到出现与给定 predicate 匹配的 目标,然后返回它。

🌐 Waits until a target matching the given predicate appears and returns it.

这将显示所有打开的浏览器上下文

🌐 This will look all open browser contexts.