# 会话检查

该 **会话检查** 工具是一个强大的 **调试功能** ，它是 Oxylabs 无头浏览器 的一项功能，利用 VNC（虚拟网络计算）技术，为你的浏览器自动化会话提供实时的可视化访问。此工具在你的无头浏览器实例与一个可供你观察和 控制的图形界面之间建立了一个安全桥梁.

### 何时使用它？

尽管 Oxylabs 无头浏览器 提供了内置的隐身能力、动态 CAPTCHA 绕过以及其他功能，但某些场景需要直接的可视化观察来：

* 诊断日志或错误消息中不明显的复杂问题
* 准确理解页面在浏览器中实际呈现的样子
* 验证你的自动化工作流是否正确执行
* 实时排查异常行为

### 使用示例

你可以通过添加以下参数来启用会话检查工具： `o_vnc=true` 到连接端点，例如：

* **Chrome 浏览器：** `wss://username:password@ubc.oxylabs.io?o_vnc=true`
* **Firefox 浏览器：** `wss://username:password@ubs.oxylabs.io?o_vnc=true`

{% hint style="success" %}
此功能同时适用于 Playwright 和 Puppeteer 库。
{% endhint %}

{% tabs %}
{% tab title="Chrome (UBC)" %}

```javascript
const {chromium} = require('playwright');

(async () => {
    let page = null;
    let browser = null;

    try {
        // 使用 o_vnc=true 参数连接
        browser = await chromium.connectOverCDP('wss://user:pass@ubc.oxylabs.io?o_vnc=true');
        const ctx = browser.contexts()[0];

        // 创建一个新页面
        page = await ctx.newPage();
        page.on('console', async msg => {
            console.log(`BROWSER [${msg.type()}] ${msg.text()}`);
        });

        // 使用 CDP 会话获取会话 ID
        let sesId = await (await ctx.newCDPSession(page)).send("__session_id");
        // 使用 UB novnc 客户端或任何支持 WebSocket 的客户端连接到 vnc
        console.log(`连接到 VNC：https://vnc.headlesify.io/novnc/?id=${sesId.value}`);

        // 打开一个页面
        await page.goto('https://duckduckgo.com');

        // 休眠 10 分钟
        await new Promise(resolve => {
            setTimeout(resolve, 6000000);
        });
    }
    catch (e) {
        console.log("结束时出错：", e);
    } finally {
        if (page != null) await page.close();
        if (browser != null) await browser.close();
        process.exit(0);
    }
})();
```

{% endtab %}

{% tab title="Firefox (UBS)" %}

```javascript
const { firefox } = require('playwright');

(async () => {
    let page = null;
    let browser = null;

    try {
        // 使用 o_vnc=true 参数连接
        browser = await firefox.connect('wss://user:pass@ubs.oxylabs.io?o_vnc=true');
        const ctx = await browser.newContext();

        // 创建一个新页面
        page = await ctx.newPage()
        page.on('console', async msg => {
            console.log(`BROWSER [${msg.type()}] ${msg.text()}`);
        })

        // 使用 CDP 会话获取会话 ID
        let sesId = await (await ctx.newCDPSession(page)).evaluate("__session_id")
        // 使用 UB novnc 客户端或任何支持 WebSocket 的客户端连接到 VNC
        console.log(`连接到 VNC：https://vnc.headlesify.io/novnc/?id=${sesId.value}`);

        // 打开一个页面
        await page.goto('https://duckduckgo.com');

        // 休眠 10 分钟
        await new Promise(resolve => {
            setTimeout(resolve, 6000000);
        })
    }
    catch (e) {
        console.log("结束时出错：", e);
    } finally {
        if (page != null) await page.close();
        if (browser != null) await browser.close();
        process.exit(0);
    }
})();
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developers.oxylabs.io/products/cn/headless-browser/features/session-inspection.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
