> For the complete documentation index, see [llms.txt](https://docs.zerowork.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.zerowork.io/building-blocks/logic/start-repeat.md).

# Start Repeat

<figure><picture><source srcset="https://3220183989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fg9LcR8XM7TGOzOCDGsCS%2Fuploads%2Fgit-blob-9ad7112f15a970e4fb6dcd83f088a8997df1bcc8%2Fstart-repeat-tile-dark.png?alt=media" media="(prefers-color-scheme: dark)"><img src="https://3220183989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fg9LcR8XM7TGOzOCDGsCS%2Fuploads%2Fgit-blob-db8f93041d2b0a0bf87d72b08df14f6ef26c812f%2Fstart-repeat-tile.png?alt=media" alt="Start Repeat block" width="279"></picture><figcaption></figcaption></figure>

**Start Repeat** starts a loop: the building blocks that follow it run once per repetition. **After Repeat** continues the flow when the loop is done, and **Break Repeat** ends the loop early.

<figure><picture><source srcset="https://3220183989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fg9LcR8XM7TGOzOCDGsCS%2Fuploads%2Fgit-blob-22da361658c0cdc70ffffb3bd9bc5164c118ad1e%2Fstart-repeat-settings-dark.png?alt=media" media="(prefers-color-scheme: dark)"><img src="https://3220183989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fg9LcR8XM7TGOzOCDGsCS%2Fuploads%2Fgit-blob-8ca61c0c75ed1da1862e71ec97dc9b66a8c9e0e9%2Fstart-repeat-settings.png?alt=media" alt="Start Repeat settings" width="555"></picture><figcaption></figcaption></figure>

### Loop Types

* **Standard**: adds new rows or repeats actions. Everything saved to a table during one repetition goes into one new row. Existing rows are ignored. Typical use: saving a list of items scraped from a website.
* **Dynamic**: processes the existing rows of a table one by one. Table columns you reference give the values of the current row, and data you save to the table updates that row. Typical use: opening each saved profile link and adding details to its row.

For a dynamic loop, select the **Table to loop through**. Optionally set a **Repetition limit** to process only some of the rows, or turn on **Newest rows first (reverse order)**.

### One Repetition, One Row

All building blocks inside the loop work on the same row during one repetition.

* **Standard**: a Start Repeat followed by four Save Web Element building blocks fills four columns of one new row per repetition, not four rows.
* **Dynamic**: all building blocks read from and write to the row the loop is currently at.

### How a Standard Loop Repeats

| Option                                 | What it does                                                                                                                                                                                                                                                            |
| -------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Fixed number of repetitions**        | Repeats the number of times you enter. Use it when you know how many repetitions you need.                                                                                                                                                                              |
| **Continue until no element is found** | Keeps going until a selector with `{loop_index}` finds no element. For long scrollable lists like social feeds. Set a **Repetition limit** so a run can't get stuck.                                                                                                    |
| **Count elements matching selector**   | Counts the elements that match the **Lead selector** once, before the loop starts, and repeats that many times. For short lists and paginated results where the number of items varies. Elements that load later are not counted, and the run fails if nothing matches. |

{% hint style="warning" %}
With **Continue until no element is found**, the loop needs at least one building block whose selector contains `{loop_index}`, otherwise it never ends. If that block does not fail on a missing element (Check Web Element, Save Web Element with **Skip if no element is found**, or a web action inside a [Try-Catch scope](https://docs.zerowork.io/building-blocks/logic/try-catch.md)), keep **Auto-scroll** on: the end of the list is then detected by scrolling.
{% endhint %}

### Loop Index

In a standard loop, write `{loop_index}` in a selector where the number of the item goes. It is replaced by the number of the current repetition: 1, 2, 3 and so on.

`ul.results > li:nth-child({loop_index}) a`

This selector targets the link in the first list item in repetition 1, in the second list item in repetition 2, and so on.

Two optional numbers change the counting: the number to start from and the step.

| Syntax             | Numbers used                                                                                                                 |
| ------------------ | ---------------------------------------------------------------------------------------------------------------------------- |
| `{loop_index}`     | 1, 2, 3, ...                                                                                                                 |
| `{loop_index,3}`   | Starts from 3: 3, 4, 5, ...                                                                                                  |
| `{loop_index,1,2}` | Starts from 1 in steps of 2: 1, 3, 5, ... Use it when the list skips items, for example when every second item is a divider. |

The default start is 1. Directly after `nth=` it is 0, because `nth=` counts from 0.

### Auto-scroll

On by default; not available with **Count elements matching selector**. When a selector with `{loop_index}` finds no element, the TaskBot scrolls the list, or the page, until the element appears. This is what makes long and infinite lists work.

The first building block with `{loop_index}` should target something every item has, such as the item's container.

### Start from and Auto-continue

* **Start from**: the row number (dynamic) or element number (standard) the loop starts at. Default is 1.
* **Auto-continue from last row / element**: the next run continues where this run stopped instead of starting over. Combine it with a number of repetitions or a repetition limit to work through a long list in batches, for example 100 rows per day.

### After Repeat

Connect **After Repeat** to the Start Repeat building block. The blocks that follow it run once, after all repetitions are done. Use it to send a notification at the end, click "Next" after a page of results, or start a second loop in the same TaskBot.

### Break Repeat

**Break Repeat** ends the current loop right away. Place it in a branch, for example after a Set Condition, or in the **Not Found** branch of Check Web Element when there is no "Next" button anymore.

### Nested Loops and Pagination

A loop can contain another loop. The typical case is paginated results:

1. The outer Start Repeat repeats once per page.
2. The inner Start Repeat saves the items on the current page.
3. The **After Repeat** of the inner loop clicks "Next". This click belongs to the outer loop, so the cycle starts again on the next page.

Inside the inner loop, `{loop_index}` counts the repetitions of the inner loop. To use the count of the outer loop there, add the ID of the outer Start Repeat building block: `{loop_index_35652}`.

For example, to read a web table cell by cell, the outer loop goes through the rows and the inner loop through the cells of each row:

`table tr:nth-child({loop_index_35652}) td:nth-child({loop_index})`

The start and step numbers work with an ID too: `{loop_index_35652,1,2}`.

### Google Sheets

Under **More options**, **Sync after ... rows** sets how often collected rows are written to a Google Sheet during the loop. Default is 50. Enter 1 to write every row right away.
