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 |
|
boolean |
这个 浏览器上下文 是否已关闭。 🌐 Whether this browser context is closed. |
|
id |
|
字符串 | 未定义 🌐 string | undefined |
此 浏览器上下文 的标识符。 🌐 Identifier for this browser context. |
方法
🌐 Methods
|
方法 🌐 Method |
修饰符 🌐 Modifiers |
描述 🌐 Description |
|---|---|---|
|
🌐 Gets the browser associated with this browser context. | ||
|
清除此 浏览器上下文 的所有权限覆盖。 🌐 Clears all permission overrides for this browser context. | ||
|
🌐 Closes this browser context and all associated pages. 附注 无法关闭默认浏览器上下文。 🌐 The default browser context cannot be closed. | ||
|
|
获取浏览器上下文中的所有 cookies。 🌐 Gets all cookies in the browser context. | |
|
|
在此浏览器上下文中删除 Cookie。 🌐 Removes cookie in this browser context. | |
|
|
在此浏览器上下文中删除与提供的过滤器匹配的 Cookie。 🌐 Deletes cookies matching the provided filters in this browser context. | |
|
🌐 Creates a new page in this browser context. | ||
|
|
在指定的 🌐 Grants this browser context the given 已弃用: 支持 BrowserContext.setPermission()。 🌐 in favor of BrowserContext.setPermission(). | |
|
🌐 Gets a list of all open pages inside this browser context. 附注 非可见的页面,例如 🌐 Non-visible pages, such as | ||
|
|
在浏览器上下文中设置 cookie。 🌐 Sets a cookie in the browser context. | |
|
为特定来源设置权限。 🌐 Sets the permission for a specific origin. | ||
|
🌐 Gets all active targets inside this browser context. | ||
|
等待直到出现与给定 🌐 Waits until a target matching the given 这将显示所有打开的浏览器上下文。 🌐 This will look all open browser contexts. |