# 浏览器指令（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="https://2655358775-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzrXw45naRpCZ0Ku9AjY1%2Fuploads%2FVVCJxliUYBEPcG34IThU%2Fbi_1.png?alt=media&#x26;token=1f689105-300d-49ce-a38a-145fd925f54f" 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="https://2655358775-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzrXw45naRpCZ0Ku9AjY1%2Fuploads%2Fskb19h4I9wV9pBJSQIP6%2Fbi_2.png?alt=media&#x26;token=36c0fa1e-7bb6-4bf6-8047-ab9d9bbd20dd" 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="../../../zhua-qu-jie-jue-fang-an/web-scraper-api/features/js-rendering-and-browser-control/browser-instructions/list-of-instructions" %}
[list-of-instructions](https://developers.oxylabs.io/documentation/cn/zhua-qu-jie-jue-fang-an/web-scraper-api/features/js-rendering-and-browser-control/browser-instructions/list-of-instructions)
{% endcontent-ref %}

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

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

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