# 浏览器指令（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: 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.
