存储与认证
🌐 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 + localStorage)保存和恢复到一个文件中。这是跨会话保持身份验证的主要方法。
🌐 Save and restore the full browser state (cookies + localStorage) in a single file. This is the primary way to persist authentication across sessions.
browser_storage_state
将当前状态保存到 JSON 文件。
🌐 Save current state to a JSON file.
→ browser_storage_state
State saved to: auth-state.json
browser_set_storage_state
恢复先前保存的状态。
🌐 Restore a previously saved state.
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
path | string | 是 | 状态 JSON 文件的路径 |
→ browser_set_storage_state { path: "./auth-state.json" }
→ browser_navigate { url: "https://app.example.com/dashboard" }
→ browser_snapshot
// Already logged in
- heading "Dashboard" [level=1]
工作流程:保存登录,下次跳过
🌐 Workflow: save login, skip it next time
// Login once
→ browser_navigate { url: "https://app.example.com/login" }
→ browser_type { ref: "e3", text: "alice@example.com" }
→ browser_type { ref: "e5", text: "s3cret!" }
→ browser_click { ref: "e7" }
→ browser_snapshot
- heading "Dashboard" [level=1]
// Save
→ browser_storage_state
State saved to: auth-state.json
// Next session: restore instead of logging in
→ browser_set_storage_state { path: "./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 | 字符串 | 否 | 按域名过滤 |
path | 字符串 | 否 | 按路径过滤 |
→ browser_cookie_list
Name Value Domain HttpOnly Secure Expires
session_id abc123 .example.com true true 2024-12-31
theme dark .example.com false false Session
_ga GA1.2... .example.com false false 2026-01-01
browser_cookie_get
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
name | 字符串 | 是 | Cookie 名称 |
browser_cookie_set
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
name | 字符串 | 是 | Cookie 名称 |
value | 字符串 | 是 | Cookie 值 |
domain | 字符串 | 否 | Cookie 域 |
path | 字符串 | 否 | Cookie 路径(默认值:/) |
expires | 数字 | 否 | 过期时间(Unix 时间戳) |
httpOnly | 布尔值 | 否 | HTTP-only 标志 |
secure | 布尔值 | 否 | 安全标志 |
sameSite | 字符串 | 否 | Strict, Lax, 或 None |
浏览器_清除Cookie / 浏览器_清空Cookie
🌐 browser_cookie_delete / browser_cookie_clear
通过名称删除特定的 Cookie,或清除所有 Cookie。
🌐 Delete a specific cookie by name, or clear all cookies.
工作流:通过清除 cookies 测试注销
🌐 Workflow: test logout by clearing cookies
→ browser_cookie_clear
→ browser_reload
→ browser_snapshot
- heading "Sign in" [level=1]
- textbox "Email" [ref=e3]
localStorage
浏览器本地存储列表 / 获取 / 设置 / 删除 / 清除
🌐 browser_localstorage_list / get / set / delete / clear
管理当前来源的 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
Key Value
user_preferences {"theme":"dark","lang":"en"}
onboarding_done true
→ browser_localstorage_set { key: "onboarding_done", value: "false" }
→ browser_reload
→ browser_snapshot
// Onboarding wizard appears
- heading "Welcome! Let's get started" [level=1]
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 | (无) |