> 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/writing-conditions.md).

# 条件编写指南

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

> ⚠️ 关于字符串（String）的核心规则：对于文字/字符串类型的数据，你必须将其包裹在双引号 `""` 中（例如：`$name = "admin"`）。对于数字则直接填写。

#### 1. 数学比较运算符（仅适用于数字）

* `>` : 大于。（例如：`$count > 5`）
* `<` : 小于。（例如：`$loopIndex < 10`）
* `>=` : 大于或等于。
* `<=` : 小于或等于。

#### 2. 逻辑比较运算符（同时适用于数字和字符串）

* `=` : 相等。
  * *示例*：`3 = 3` ➡️ `true`
  * *示例*：`"hello" = "hola"` ➡️ `false`
* `!=` : 不相等（等于运算的否定）。
  * *示例*：`3 != 4` ➡️ `true`
  * *示例*：`"hello" != "hello"` ➡️ `false`

#### 3. 字符串（String）处理运算符

* `contains` : 检查前面的字符串是否包含后面的字符串。
  * *示例*：`"ABCD" contains "AB"` ➡️ `true`
  * *示例*：`"AB" contains "ABCD"` ➡️ `false`
* `!contains` : 检查前面的字符串是否 *不* 包含后面的字符串。
  * *示例*：`!"ABCD" contains "AB"` ➡️ `false`
  * *示例*：`!"AB" contains "ABCD"` ➡️ `true`
* `contains "B"`（单独出现在 B 前面时）：检查需要判断的变量或字符串是否为空字符串。
  * *结果*：如果为空则返回 `true`，如果包含数据则返回 `false`。

#### 4. 检查界面元素（Element）的函数

此函数通过 XPath 路径帮助脚本识别网页上按钮、输入框等的出现情况：

* `hasElement(XPATH)` : 当页面上存在该元素时，条件为真。
* `!hasElement(XPATH)` : 当页面上不存在该元素时，条件为真（`hasElement` 的否定）。

#### 5. 组合多个复杂条件的方法

你可以使用逻辑符号和圆括号 `()` 将多个条件组合在同一行中，以进行分组：

* `&&` : 与（AND）运算（所有条件必须同时为真）。
* `||` : 或（OR）运算（只要有一个条件为真即可）。

编写条件时的实际示例：

* *情况 1*：检查循环次数是否大于 5，且账户名称是否包含 "abc" 字样：

  ```
  $count > 5 && $name contains "abc"
  ```
* *情况 2*：检查是否没有出现登录按钮（已登录）或者错误提示变量显示为 "true"：

  ```
  !hasElement(//div[@id='x']) || $error = "true"
  ```


---

# 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/writing-conditions.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.
