> ## Documentation Index
> Fetch the complete documentation index at: https://help.decodo.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 浏览器操作

> 在抓取前自动执行页面内交互：click、input、scroll、wait、wait_for_element 和 fetch_resource，支持 CSS、XPath 和文本选择器。

网页数据 API 支持在获取所需结果之前执行多种浏览器操作。

## 操作

### `click`

| 名称 | 参数 | 描述 |
| - | - | - |
| `click` | 选择器：`type`："xpath"/"css"/"text" `value`：字符串 | 对指定元素执行点击操作，并等待设定的秒数。 |

**示例：**

<CodeGroup>
  ```json JSON theme={null}
  {
      "target": "universal",
      "url": "https://www.yahoo.com/",
      "browser_actions": [
          {
              "type": "click",
              "selector": {
                "type": "xpath",
                "value": "//button"
              }
          }
      ]
  }
  ```
</CodeGroup>

### `input`

| 名称 | 参数 | 描述 |
| - | - | - |
| `input` | 选择器：\*`type`："xpath"/"css"/"text" \*`value`：字符串 `value`：字符串 | 将文本输入到页面上指定的输入元素中。 |

**示例：**

<CodeGroup>
  ```json JSON theme={null}
  {
      "target": "universal",
      "url": "https://www.yahoo.com/",
      "browser_actions": [
          {
              "type": "input",
              "selector": {
                  "type": "xpath",
                  "value": "//input"
              },
  	"value": "Hello world"	
          }
      ]
  }
  ```
</CodeGroup>

### `scroll`

<table>
  <colgroup>
    <col width="100" />

    <col width="194" />

    <col width="418" />
  </colgroup>

  <thead>
    <tr>
      <th>名称</th>
      <th>参数</th>
      <th>描述</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`scroll`</td>
      <td>`x`：整数 `y`：整数</td>
      <td>将页面内容滚动指定的像素数。</td>
    </tr>
  </tbody>
</table>

**示例：**

<CodeGroup>
  ```json JSON theme={null}
  {
      "target": "universal",
      "url": "https://www.yahoo.com/",
      "browser_actions": [
          {
              "type": "scroll",
              "x": "0",
              "y": "300"
          }
      ]
  }
  ```
</CodeGroup>

### `scroll_to_bottom`

<table>
  <colgroup>
    <col width="156" />

    <col width="166" />

    <col width="392" />
  </colgroup>

  <thead>
    <tr>
      <th>名称</th>
      <th>参数</th>
      <th>描述</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`scroll_to_bottom`</td>
      <td>`timeout_s`：整数</td>
      <td>在设定的秒数内持续向下滚动页面。</td>
    </tr>
  </tbody>
</table>

**示例：**

<CodeGroup>
  ```json JSON theme={null}
  {
      "target": "universal",
      "url": "https://www.yahoo.com/",
      "browser_actions": [
          {
              "type": "scroll_to_bottom",
              "timeout_s": 5
          }
      ]
  }
  ```
</CodeGroup>

### `wait`

<table>
  <colgroup>
    <col width="100" />

    <col width="182" />

    <col width="432" />
  </colgroup>

  <thead>
    <tr>
      <th>名称</th>
      <th>参数</th>
      <th>描述</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`wait`</td>
      <td>`wait_time_s`：整数</td>
      <td>暂停指定的秒数。</td>
    </tr>
  </tbody>
</table>

**示例：**

<CodeGroup>
  ```json JSON theme={null}
  {
      "target": "universal",
      "url": "https://www.yahoo.com/",
      "browser_actions": [
          {
              "type": "wait",
              "wait_time_s": 5
          }
      ]
  }
  ```
</CodeGroup>

### `wait_for_element`

<table>
  <colgroup>
    <col width="157" />

    <col width="288" />

    <col width="270" />
  </colgroup>

  <thead>
    <tr>
      <th>名称</th>
      <th>参数</th>
      <th>描述</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`wait_for_element`</td>
      <td>选择器：\*`type`："xpath"/"css"/"text" \*`value`：字符串 `timeout_s`：整数</td>
      <td>在指定时长内等待元素加载。</td>
    </tr>
  </tbody>
</table>

**示例：**

<CodeGroup>
  ```json JSON theme={null}
  {
      "target": "universal",
      "url": "https://www.yahoo.com/",
      "browser_actions": [
          {
              "type": "wait_for_element",
              "selector": {
                  "type": "css",
                  "value": ".submit-button"
              },
  	"timeout_s": 5
          }
      ]
  }
  ```
</CodeGroup>

### `fetch_resource`

<Warning>
  如果与其他指令一起使用，`fetch_resource` 应作为最后一个操作。
</Warning>

<table>
  <colgroup>
    <col width="140" />

    <col width="304" />

    <col width="280" />
  </colgroup>

  <thead>
    <tr>
      <th>名称</th>
      <th>参数</th>
      <th>描述</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`fetch_resource`</td>
      <td>`filter`：正则表达式 `on_error`："error"/"skip"</td>
      <td>获取与指定模式匹配的第一个 Fetch 或 XHR 资源</td>
    </tr>
  </tbody>
</table>

**示例：**

<CodeGroup>
  ```json JSON theme={null}
  {
      "target": "universal",
      "url": "https://www.yahoo.com/",
      "browser_actions": [
          {
              "type": "fetch_resource",
              "filter": "https://api.example.com/products/*",          
              "on_error": "error"
          }
      ]
  }
  ```
</CodeGroup>

## 通用参数

以下参数适用于上述所有操作。

### `type`

<table>
  <colgroup>
    <col width="100" />

    <col width="634" />
  </colgroup>

  <thead>
    <tr>
      <th>名称</th>
      <th>描述</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`type`</td>
      <td>所使用的浏览器操作类型</td>
    </tr>
  </tbody>
</table>

### `timeout_s`

<table>
  <colgroup>
    <col width="106" />

    <col width="630" />
  </colgroup>

  <thead>
    <tr>
      <th>名称</th>
      <th>描述</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`timeout_s`</td>
      <td>操作执行的最长等待时间（秒），超时后将终止该操作。</td>
    </tr>
  </tbody>
</table>

### `wait_time_s`

<table>
  <colgroup>
    <col width="122" />

    <col width="617" />
  </colgroup>

  <thead>
    <tr>
      <th>名称</th>
      <th>描述</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`wait_time_s`</td>
      <td>明确指定用于执行该操作的时间（秒）。</td>
    </tr>
  </tbody>
</table>

### `on_error`

<table>
  <colgroup>
    <col width="100" />

    <col width="640" />
  </colgroup>

  <thead>
    <tr>
      <th>名称</th>
      <th>描述</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`on_error`</td>
      <td>指定操作失败时的处理方式："error"：停止执行浏览器操作。"skip"：继续执行下一个操作。</td>
    </tr>
  </tbody>
</table>

## 获取网络请求

如果网站通过获取 `JSON` 对象来填充内容，您可以只抓取该网络请求，从而完全无需处理 `HTML`。为此，您可以使用 `fetch_resource` 浏览器操作，如下所示。

<Warning>
  `fetch_resource` 不能与任何其他指令组合使用，只能在单独的请求中使用。
</Warning>

例如，加载 yahoo.com 并打开“网络”（Network）选项卡时，可以看到正在加载一个 `exp.json` 文件：

<Frame>
  <img src="https://mintcdn.com/decodo/HeowWjVaMvMCJRIK/images/docs/539e0b0-how_to_use_spoons_2024-05-10_at_13.02.09.png?fit=max&auto=format&n=HeowWjVaMvMCJRIK&q=85&s=9e768d6d99a4e5007241ba7cc8a0c7ba" alt="" width="1432" height="1171" data-path="images/docs/539e0b0-how_to_use_spoons_2024-05-10_at_13.02.09.png" />
</Frame>

如果您只想抓取此请求的内容，可以使用 `fetch_resource` 浏览器操作。请注意，`filter` 是用于匹配文件名的正则表达式：

<CodeGroup>
  ```json Payload theme={null}
  {
      "target": "universal",
      "url": "https://www.yahoo.com/",
      "browser_actions": [
          {
              "type": "fetch_resource",
              "filter": "/ybar/exp"
          }
      ]
  }
  ```
</CodeGroup>

结果：

<CodeGroup>
  ```json Payload theme={null}
  {
      "results": [
          {
              "content": "{\n    \"expCount\":5,\n    \"selection\":\"individual\",\n ... }",
              "status_code": 200,
              "url": "https://example.com/api/product/1",
              "task_id": "7131940420107377665",
              "created_at": "2023-11-19 09:46:41",
              "updated_at": "2023-11-19 09:47:08"
          }
      ]
  }
  ```
</CodeGroup>

***

<Columns cols={2}>
  <Card title="支持" href="https://direct.lc.chat/12092754" cta="让我们聊聊！">
    需要帮助或只是想打个招呼？我们的支持团队全天候为您服务。 \
    您也可以随时通过电子邮件 [support@decodo.com](mailto:support@decodo.com) 联系我们。
  </Card>

  <Card title="反馈" href="mailto:feedback@decodo.com" cta="分享反馈">
    找不到您要找的内容？请求一篇文章！ \
    有反馈意见？分享您对我们如何改进的想法。
  </Card>
</Columns>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.