> 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/cao-zuo-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）：看到“用户名/密码”输入框（账户已注销）。
  * *操作*：从变量中调用数据以填写账户、密码并点击登录。
* 否则所有（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/cao-zuo-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.
