存储与认证
🌐 Storage & Authentication
管理 cookies、localStorage、sessionStorage,并保存/恢复完整浏览器状态以保持认证持续性。需要 storage 能力。
🌐 Manage cookies, localStorage, sessionStorage, and save/restore full browser state for authentication persistence. Requires the storage capability.
{
"mcpServers": {
"playwright": {
"command": "npx",
"args": ["@playwright/mcp@latest", "--caps=storage"]
}
}
}
存储状态
🌐 Storage state
将完整的浏览器状态(包括 Cookies 和本地存储)保存到一个文件中,并可以恢复。这是跨会话保持登录的主要方法。
🌐 Save and restore the full browser state (cookies + local storage) in a single file. This is the primary way to persist authentication across sessions.
browser_storage_state
把当前状态保存到一个 JSON 文件里。
🌐 Save the current state to a JSON file.
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
filename | 字符串 | 否 | 要保存的文件。相对名称相对于工作区根目录解析。默认为输出目录下的 storage-state-{timestamp}.json |
→ browser_storage_state { filename: "auth-state.json" }
- [Storage state](auth-state.json)
browser_set_storage_state
恢复之前保存的状态。现有的 Cookies 和本地存储会先被清除。
🌐 Restore a previously saved state. Existing cookies and local storage are cleared first.
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
filename | 字符串 | 是 | 存储状态文件的路径。相对名称相对于工作区根目录解析 |
→ browser_set_storage_state { filename: "auth-state.json" }
Storage state restored from auth-state.json
→ browser_navigate { url: "https://app.example.com/dashboard" }
// Already logged in
- heading "Dashboard" [level=1] [ref=e2]
工作流程:保存登录,下次跳过
🌐 Workflow: save login, skip it next time
// Login once
→ browser_navigate { url: "https://app.example.com/login" }
→ browser_type { target: "e3", text: "alice@example.com" }
→ browser_type { target: "e5", text: "s3cret!" }
→ browser_click { target: "e7" }
- heading "Dashboard" [level=1] [ref=e2]
// Save
→ browser_storage_state { filename: "auth-state.json" }
// Next session: restore instead of logging in
→ browser_set_storage_state { filename: "auth-state.json" }
→ browser_navigate { url: "https://app.example.com/dashboard" }
或者在服务器启动时自动加载状态:
🌐 Or load state automatically on server startup:
["@playwright/mcp@latest", "--caps=storage", "--isolated", "--storage-state=./auth-state.json"]
Cookies
browser_cookie_list
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
domain | 字符串 | 否 | 按域名筛选 cookies |
path | 字符串 | 否 | 按路径筛选 cookies |
→ browser_cookie_list
session_id=abc123 (domain: .example.com, path: /)
theme=dark (domain: .example.com, path: /)
browser_cookie_get
| 参数 | 类型 | 是否必填 | 描述 |
|---|---|---|---|
name | 字符串 | 是 | 要获取的 Cookie 名称 |
→ browser_cookie_get { name: "session_id" }
session_id=abc123 (domain: .example.com, path: /, httpOnly: true, secure: true, sameSite: Lax)
browser_cookie_set
域默认使用当前页面的主机名,路径默认使用 /。
🌐 Domain defaults to the current page's hostname and path defaults to /.
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
name | 字符串 | 是 | Cookie 名称 |
value | 字符串 | 是 | Cookie 值 |
domain | 字符串 | 否 | Cookie 域名 |
path | 字符串 | 否 | Cookie 路径 |
expires | 数字 | 否 | 以 Unix 时间戳表示的过期时间 |
httpOnly | 布尔值 | 否 | 是否仅限 HTTP |
secure | 布尔值 | 否 | 是否安全 |
sameSite | 字符串 | 否 | Strict、Lax 或 None |
浏览器_清除Cookie / 浏览器_清空Cookie
🌐 browser_cookie_delete / browser_cookie_clear
通过 name 删除特定的 cookie,或者清除所有 cookie。
🌐 Delete a specific cookie by name, or clear all cookies.
工作流:通过清除 cookies 测试注销
🌐 Workflow: test logout by clearing cookies
→ browser_cookie_clear
→ browser_navigate { url: "https://app.example.com/dashboard" }
- heading "Sign in" [level=1] [ref=e2]
- textbox "Email" [ref=e3]
localStorage
管理当前来源的 localStorage 键值对。
🌐 Manage localStorage key-value pairs for the current origin.
| 工具 | 参数 |
|---|---|
browser_localstorage_list | (无) |
browser_localstorage_get | key(字符串) |
browser_localstorage_set | key(字符串),value(字符串) |
browser_localstorage_delete | key(字符串) |
browser_localstorage_clear | (无) |
→ browser_localstorage_list
user_preferences={"theme":"dark","lang":"en"}
onboarding_done=true
→ browser_localstorage_set { key: "onboarding_done", value: "false" }
→ browser_navigate { url: "https://app.example.com" }
// Onboarding wizard appears
- heading "Welcome! Let's get started" [level=1] [ref=e2]
sessionStorage
与 localStorage 相同的接口,但作用域为会话 — 当标签页关闭时数据会被清除。
🌐 Same interface as localStorage but session-scoped — data is cleared when the tab closes.
| 工具 | 参数 |
|---|---|
browser_sessionstorage_list | (无) |
browser_sessionstorage_get | key(字符串) |
browser_sessionstorage_set | key(字符串),value(字符串) |
browser_sessionstorage_delete | key(字符串) |
browser_sessionstorage_clear | (无) |