> For the complete documentation index, see [llms.txt](https://developers.oxylabs.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers.oxylabs.io/products/cn/agent-browser/persistent-sessions-and-profiles.md). Agents can also search and read this documentation through its [MCP server](https://developers.oxylabs.io/~gitbook/mcp).

# 持久会话和配置文件

了解 Oxylabs 智能体浏览器上的持久（粘性）会话和持久配置文件。重新连接到进行中的会话，并在多个会话中复用您的 Cookie 或本地存储。

管理长时间运行的多阶段自动化工作流，或在不同连接序列之间维护已登录用户状态，需要持久化浏览器状态。Oxylabs Agent Browser 提供两种高级持久化系统：

<table data-header-hidden><thead><tr><th width="178"></th><th></th></tr></thead><tbody><tr><td><a href="#persistent-sessions-sticky-sessions"><strong>持久会话</strong></a></td><td>在客户端断开连接期间，保持远程浏览器环境持续运行。</td></tr><tr><td><a href="#persistent-profiles"><strong>持久配置文件</strong></a></td><td>保存和恢复 Cookie、本地存储和浏览器缓存。</td></tr></tbody></table>

## 持久会话

持久（粘性）会话通过定义自定义 `session_name` 到连接 URL：

```bash
wss://USER:PASS@hb.oxylabs.io?session_name=checkout-flow-test
```

{% hint style="info" %}
持久会话 **默认启用** ，适用于所有用户，默认限制为 **5 个并发会话**。如果你需要更高的限制，请联系 Oxylabs 支持（[在线聊天](https://oxylabs.io/) 或 [电子邮件](mailto:support@oxylabs.io)）或你的专属客户经理。
{% endhint %}

<table><thead><tr><th width="127">参数</th><th width="534">说明</th><th width="98">类型</th></tr></thead><tbody><tr><td><code>session_name</code></td><td>定义后，会话可持续至最大生存时间 (TTL)（最长 24 小时或直到停止）。使用相同的 <code>session_name</code> 重新连接，以重新附加同一会话 ID 和现有标签页。支持的格式： <code>^[A-Za-z0-9-]{3,36}$</code></td><td>字符串</td></tr><tr><td><code>keep_alive</code></td><td>当 <code>false</code> （默认）时，调用 <code>browser.close()</code> 会回收并关闭会话。\n当 <code>true</code>时，断开连接后会话仍会保留在云端（最长 10 分钟）。\n需要 <code>session_name</code>.</td><td>布尔值</td></tr></tbody></table>

### 重新连接到现有标签页

```javascript
// Reconnect to existing session
const browser = await chromium.connectOverCDP(endpoint);
const ctx = browser.contexts()[0];
const page = ctx.pages().find(p => p.url().includes("http://example.com"));
```

## 持久配置文件

持久配置文件 **默认启用** ，适用于所有用户，默认限制为 **5 个配置文件**。可用于在命名配置文件下保存 Cookie 和 localStorage，并在全新的会话中恢复它们。以下参数可让你在各会话间维持登录状态，而无需每次重新认证：

<table><thead><tr><th width="191">参数</th><th width="456">说明</th><th width="102">类型</th></tr></thead><tbody><tr><td><code>o_profile</code></td><td>使用命名配置文件保存/恢复 Cookie 和 localStorage（距上次使用 14 天）。支持的格式： <code>^[a-zA-Z0-9][a-zA-Z0-9_-]{2,35}$</code></td><td>字符串</td></tr><tr><td><code>o_profile_save</code></td><td>在会话期间或结束时强制保存配置文件。默认： <code>false</code>.</td><td>布尔值</td></tr><tr><td><code>proxy_resi_ses_id</code></td><td>用于在各会话间固定住宅出口 IP 的自定义会话 ID。支持的格式： <code>^[A-Za-z0-9]{3,36}$</code>.</td><td>字符串</td></tr><tr><td><code>proxy_resi_ses_time</code></td><td>保持固定住宅代理出口 IP 的时长（分钟）。最小 <code>1</code>，最大 <code>1440</code> （24 小时）。</td><td>整数</td></tr></tbody></table>

{% hint style="info" %}
超过 `max_profiles` 限制 `5` 将返回 `403 配置文件上限已达（最大值）：配置文件上限已达` 错误。你可以直接在 Dashboard（`hb.oxylabs.io/dashboard`）中查看并删除已保存的配置文件，如果你达到上限，或者重复使用一个稳定的配置文件名。联系 Oxylabs 支持（[在线聊天](https://oxylabs.io/) 或 [电子邮件](mailto:support@oxylabs.io)）或你的专属客户经理，如果需要更高的限制。
{% endhint %}

### 可靠的跨会话恢复 <a href="#reliable-cross-session-restore" id="reliable-cross-session-restore"></a>

对于具有严格机器人监控系统的网站，仅保存 Cookie 还不够。如果恢复的配置文件从不同的住宅出口 IP 连接，你的 Cookie 可能会失效，导致空白页面或安全验证界面。

为确保登录一致性，请将你的配置文件与固定的住宅出口 IP 配对：

```javascript
// Session 1: Authenticate and back up state
const connectionOne = "wss://USER:PASS@hb.oxylabs.io?o_profile=my_user_profile&proxy_resi_ses_id=ip_session_abc&proxy_resi_ses_time=120";

// Session 2: Restore profile with the exact same IP session context
const connectionTwo = "wss://USER:PASS@hb.oxylabs.io?o_profile=my_user_profile&proxy_resi_ses_id=ip_session_abc&proxy_resi_ses_time=120";
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://developers.oxylabs.io/products/cn/agent-browser/persistent-sessions-and-profiles.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
