# Loop

### 'loop' keyword

When a layer contains the loop keyword, the script will directly enter the script within the loop and loop based on the while or cnt conditions. After the loop terminates, it will skip the current layer and enter the next layer to execute the action method of the next layer.

### loop condition

The loop keyword has two conditions: 'while' and 'cnt'. The while condition can be evaluated in conjunction with the condition method. If the condition method is false, the loop is terminated. The 'cnt' condition is a counter, which means the script will loop 'cnt' times. If both condition and cnt conditions occur simultaneously, both conditions need to be met before the loop can continue.

### loop script

The loop script needs to be passed in using script keywords within the loop, supporting JSON, dict, and list types

### loop example

Note: Loop can be nested multiple times

```json
step = [{
    "method": "redirect",
    "url": "https://www.bchrt.com/tools/click-counter/",
}, {
    "loop": {
        "while": {
            "condition": "checkNum",
            "xpath": "//*[@id=\"count\"]"
        },
        "script": [{
            "loop": {
                "cnt": 5,
                "script": {
                    "method": "click",
                    "xpath": "//*[@id=\"addbtn\"]",
                },
            }
        },
            {
                "method": "click",
                "xpath": "//*[@id=\"subbtn\"]",
            }
        ]
    }
}, {
    "method": "getAttribute",
    "xpath": "//*[@id=\"count\"]",
    "name": "value"
}]
```


---

# Agent Instructions: 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:

```
GET https://wwydev.gitbook.io/crawlipt/zhi-nan/loop.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
