> For the complete documentation index, see [llms.txt](https://docs.gpmautomate.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.gpmautomate.com/automate-zh/action-shi-yong-zhi-nan/blocks/if-else-if-and-else.md).

# If、Else if 和 Else

这是一组用于根据运行时的实际条件对脚本进行分支处理的指令块。系统不会从上到下一路执行，而是会检查您设定的条件是真还是假，从而决定脚本接下来的走向。换句话说，这组指令块就像是决策大脑，帮助脚本灵活地自动处理浏览器上出现的各种变化情况。

🎥 观看更多教程视频：[点击这里](https://youtu.be/ZFS_82u23Cs)。

#### 1. 条件指令块（If）

If 是首先检查一个指定条件的指令块。如果该条件为真，系统将执行放置在此 If 块内的所有操作。如果为假，系统将跳过并继续检查下面的内容。

#### 2. 附加条件指令块（Else if）

Else if 总是跟在 If 块之后（或跟在另一个 Else if 块之后）。当其上方的所有条件都为假时，该块才会被触发。系统将检查它自身的条件，如果为真则执行内部的操作，如果为假则再次跳过。

> 您可以连续添加多个 Else if 块，以检查脚本中不同的多种情况。

#### 3. 否定指令块（Else）

Else 是条件链中最后的兜底指令块，不需要配置任何条件。当上方的所有 If 和 Else if 块都为假时（没有任何情况满足条件），系统将自动进入并执行 Else 块内部的操作。

<figure><img src="/files/TZqxgyKs35hbkMfGiiCB" alt=""><figcaption></figcaption></figure>

#### 实际示例：检查账户的登录状态

当您打开一个网页时（例如：Facebook、X 等），profile 上账户的状态可能会有所不同。您可以使用 If - Else if - Else 链来顺畅地处理各种情况：

* 如果（If）：看到"创建帖子"按钮（表明账户已经登录）。
  * *操作*：直接填写内容并发布帖子。
* 否则如果（Else if）：看到"用户名 / 密码"输入框（账户已被登出）。
  * *操作*：从 Variables 中调用数据来填写账户、密码并点击登录。
* 否则全部（Else）：既没看到发布帖子按钮，也没看到登录框（账户可能已被检查点锁定或出现网络错误）。
  * *操作*：使用 Stop 命令停止程序，或使用 Screenshot 进行截图以便日后检查。


---

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.gpmautomate.com/automate-zh/action-shi-yong-zhi-nan/blocks/if-else-if-and-else.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
