# 浏览器指令（Beta）

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

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

### 如何使用？

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

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

<figure><img src="/files/33a1c50d81d733ff45ff7625ae5cb4ad1438a29d" 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/2fd846cc402a988e2a332c713afce0891f03a4d6" 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/f0e44fde3f6abb8cc8320f6f2fbf8f0c82d9de6a" %}
[指令列表](/documentation/cn/zhua-qu-jie-jue-fang-an/web-scraper-api/features/js-rendering-and-browser-control/browser-instructions/list-of-instructions.md)
{% endcontent-ref %}

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

请参阅我们列出的响应码 [**此处**](/documentation/cn/gao-ji-dai-li-jie-jue-fang-an/web-unblocker/response-codes.md).

关于指令校验的状态码已有文档说明 [**此处**](/documentation/cn/zhua-qu-jie-jue-fang-an/web-scraper-api/features/js-rendering-and-browser-control/browser-instructions/list-of-instructions.md#instructions-validation).


---

# 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/documentation/cn/gao-ji-dai-li-jie-jue-fang-an/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.
