> 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/web-unblocker/custom-browser-instructions/browser-instructions-beta.md).

# 浏览器指令（Beta）

使用自定义浏览器指令时，您可以定义自己的特定浏览器指令，这些指令会在渲染 JavaScript 时执行。

{% hint style="success" %}
生成 **自动生成浏览器指令** 在 [**网页爬虫API Playground**](https://dashboard.oxylabs.io/?route=/api-playground) 在 Oxylabs 控制面板中。设置浏览器操作，进行测试，并以 JSON 格式导出生成的指令。&#x20;
{% endhint %}

### 如何使用？

要使用浏览器指令，请提供一组 `browser_instructions` 在创建任务时。

假设您想搜索词 `pizza boxes` 在某个网站中。

<figure><img src="/files/03fa85fee5fb2d4cf6db26ad65d017b1d606ff1f" alt=""><figcaption></figcaption></figure>

示例任务参数如下：

{% code overflow="wrap" %}

```bash
curl -k -x https://unblock.oxylabs.io:60000 \
-U 'USERNAME:PASSWORD' \
'https://www.ebay.com' \
-H 'x-oxylabs-render: html' \
-H "x-oxylabs-browser-instructions: [{\"type\":\"input\",\"value\":\"pizza boxes\",\"selector\":{\"type\":\"xpath\",\"value\":\"\/\/input[@class='gh-tb ui-autocomplete-input']\"}},{\"type\":\"click\",\"selector\":{\"type\":\"xpath\",\"value\":\"\/\/input[@type='submit']\"}},{\"type\":\"wait\",\"wait_time_s\":5}]"
```

{% endcode %}

**步骤 1。** 您必须提供 `x-oxylabs-render: html` 参数。

**步骤 2。** 浏览器指令应在 `x-oxylabs-browser_instructions` 字段中描述。

{% hint style="info" %}
作为请求头值提供的浏览器指令必须进行 JSON 转义，且不得包含多余空格。
{% endhint %}

上面的示例浏览器指令说明，目标是将搜索词 `pizza boxes` 输入搜索字段，点击 `搜索` 按钮，并等待 5 秒让内容加载。

抓取结果应如下所示：

```html
<!doctype html><html>
执行指令后的内容      
</html>
```

抓取到的 HTML 应如下所示：

<figure><img src="/files/fcbcfe381d35374d52ef46c09cb1afe43330910b" alt=""><figcaption></figcaption></figure>

#### 获取浏览器资源 <a href="#fetching-browser-resources" id="fetching-browser-resources"></a>

我们提供了一个独立的浏览器指令用于获取浏览器资源。

函数定义如下：

使用 `fetch_resource` 将导致任务返回与所提供格式匹配的第一个 Fetch/XHR 资源，而不是正在目标页面上的 HTML。

假设我们想要定位在浏览器中以自然方式访问产品页时获取的 GraphQL 资源。我们将提供如下任务信息：

{% code overflow="wrap" %}

```bash
curl -k -x https://unblock.oxylabs.io:60000 \
-U 'USERNAME:PASSWORD' \
'https://www.example.com/product-page/123' \
-H 'x-oxylabs-render: html' \
-H "x-oxylabs-browser-instructions: [{\"type\": \"fetch_resource\",\"filter\": \"\/graphql\/product-info\/123\"}]"
```

{% endcode %}

这些指令将产生如下结果：

```html
{"product_id": 123, "description": "", "price": 456}
```

### 支持的浏览器指令列表 <a href="#list-of-supported-browser-instructions" id="list-of-supported-browser-instructions"></a>

{% content-ref url="/pages/3da529542c2d18c613b1a525622208d8622cb51f" %}
[指令列表](/products/cn/web-unblocker/custom-browser-instructions/browser-instructions-beta/list-of-instructions.md)
{% endcontent-ref %}

### 状态码 <a href="#status-codes" id="status-codes"></a>

请参阅我们概述的响应码 [**此处**](/products/cn/web-unblocker/response-codes.md)。关于指令验证的状态码已记录 [**此处**](/products/cn/web-unblocker/custom-browser-instructions/browser-instructions-beta/list-of-instructions.md#instructions-validation).


---

# 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 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/web-unblocker/custom-browser-instructions/browser-instructions-beta.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.
