> 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 格式导出生成的指令。
{% endhint %}

## 如何使用？

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

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

<figure><img src="https://3714446197-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBQ7Zf9paoN3FTeGcyfY1%2Fuploads%2Fgit-blob-f4d808b71a0a56ac417c6c71f46b5a53d1401f29%2Fbi_1.png?alt=media" 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` 在搜索字段中，点击 `search` 按钮，并等待 5 秒让内容加载。

抓取结果应如下所示：

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

抓取的 HTML 应如下所示：

<figure><img src="https://3714446197-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBQ7Zf9paoN3FTeGcyfY1%2Fuploads%2Fgit-blob-15c3241746723aefc211766dbf8c4cb5549114ff%2Fbi_2.png?alt=media" 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>

### 通用参数

以下定义的所有指令都使用一致的一组参数。参数如下。

#### `type` <a href="#type" id="type"></a>

* **类型**: `Enum["click", "input", "scroll", "scroll_to_bottom", "wait", "wait_for_element", "fetch_resource"]`
* **说明：** 浏览器指令类型。

#### `timeout_s` <a href="#timeout_s" id="timeout_s"></a>

* **类型**: `整数`
* **说明：** 如果在规定时间内未完成，操作将在多久后被跳过。
* **限制**: 0 < `timeout_s` <= 60
* **默认值**: 5

#### `wait_time_s` <a href="#wait_time_s" id="wait_time_s"></a>

* **类型**: `整数`
* **说明：** 执行下一步操作前等待多久。
* **限制**: 0 < `wait_time_s` <= 60
* **默认值**: 0

#### `on_error` <a href="#on_error" id="on_error"></a>

* **类型**: `Enum["error", "skip"]`
* **说明：** 指示此指令失败时如何处理后续指令：
  * `"error"`：停止执行浏览器指令。
  * `"skip"`：继续执行下一条指令。
* **默认值**: `"error"`

#### 通用参数示例

{% code overflow="wrap" %}

```bash
{\"type\": \"wait_for_element\", \"selector\": {\"type\": \"text\", \"value\": \"Load More Items\"},\"timeout_s\": 5, \"wait_time_s\": 2, \"on_error\": \"skip\"}
```

{% endcode %}

### 说明 <a href="#click" id="click"></a>

#### `click` <a href="#click" id="click"></a>

* **说明**：点击一个元素并等待指定秒数。
* **参数：**
  * `type: str = "click"`
  * `selector: dict`
    * `type: Enum["xpath", "css", "text"]`
    * `value: str`
* **示例**:

{% code overflow="wrap" %}

```bash
{\"type\": \"click\",\"selector\": {\"type\": \"xpath\",\"value\": \"\/\/button\"}}
```

{% endcode %}

#### `input` <a href="#input" id="input"></a>

* **说明**：向选定元素输入文本。
* **参数：**
  * `type: str = "input"`
  * `selector: dict`
    * `type: Enum["xpath", "css", "text"]`
    * `value: str`
  * `value: str`
  * **示例：**

{% code overflow="wrap" %}

```bash
{\"type\": \"input\",\"selector\": {\"type\": \"xpath\",\"value\": \"\/\/input\"},\"value\": \"pizza boxes\"}
```

{% endcode %}

#### `scroll` <a href="#scroll" id="scroll"></a>

* **说明**：滚动指定像素数。
* **参数：**
  * `type: str = "scroll"`
  * `x: int`
  * `y: int`
* **示例：**

```bash
{\"type\": \"scroll\",\"x\": 0,\"y\": 100}
```

#### `scroll_to_bottom` <a href="#scroll_to_bottom" id="scroll_to_bottom"></a>

* **说明**：向下滚动到底部，持续指定秒数。
* **参数：**
  * `type: str = "scroll_to_bottom"`
* **示例**:

```bash
{\"type\": \"scroll_to_bottom\",\"timeout_s\": 10}
```

### `wait` <a href="#wait" id="wait"></a>

* **说明**：等待指定秒数。
* **参数：**
  * `type: str = "wait"`
* **示例**:

```bash
{\"type\": \"wait\",\"wait_time_s\": 2}
```

#### `wait_for_element` <a href="#wait_for_element" id="wait_for_element"></a>

* **说明**：等待元素加载，持续指定秒数。
* **参数：**
  * `type: str = "wait_for_element"`
  * `selector: dict`
    * `type: Enum["xpath", "css", "text"]`
    * `value: str`
  * **示例：**

{% code overflow="wrap" %}

```bash
{\"type\": \"wait_for_element\",\"selector\": {\"type\": \"text\",\"value\": \"Load More Items\"},\"timeout_s\": 5}
```

{% endcode %}

#### `fetch_resource` <a href="#fetch_resource" id="fetch_resource"></a>

{% hint style="warning" %}
该 `fetch_resource` 该指令必须是浏览器指令列表中的最后一条；后续任何指令都不会执行。
{% endhint %}

* **说明**：获取与所设模式匹配的第一个 Fetch/XHR 资源。
* **参数：**
  * `type: str = "fetch_resource"`
  * `filter: str(RegEx expression)`
  * `on_error: Enum["error", "skip"]`
* **示例：**

```bash
{\"type\": \"fetch_resource\",\"filter\": \"\/graphql\/item\/\"}
```

### 指令验证

指令格式的任何不一致都将导致 `400` 状态码和相应的错误消息。

例如，以下载荷：

```bash
[{\"type\": \"unsupported-wait\",\"wait_time_s\": 5}]}]
```

将导致：

```json
{    
    "errors": {
        "message": "Unsupported action type `unsupported-wait`, choose from 'click,fetch_resource,input,scroll,scroll_to_bottom,wait,wait_for_element'"
    }
}
```

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

请参见我们列出的响应代码 [**这里**](https://developers.oxylabs.io/products/cn/web-unblocker/response-codes.md).


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://developers.oxylabs.io/products/cn/web-unblocker/custom-browser-instructions/browser-instructions-beta.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
