Understanding Loops
Modern websites are rarely static. Pages load content dynamically, lists change, and user actions often determine when a workflow should stop.
Doppelganger provides control-flow blocks to handle these scenarios, letting you automate complex tasks reliably.
This guide covers:
Repeat N→ repeat actions a fixed number of timesWhile→ repeat actions until a condition is falseFor Each→ repeat actions for every item in a list or CSVEnd Block→ defines where loops stop controlling execution
Every loop or conditional block must end with an End Block, which tells Doppelganger where to continue normal execution.
Control Flow Blocks Overview#
Repeat N→ fixed count loopWhile→ condition-based loop (selector or variable)For Each→ list or CSV-based loopEnd Block→ closes loops and defines scope
Each loop must end with an End Block.
Without it, Doppelganger cannot determine which actions are repeated.
The End Block (Required)#
The End Block signals where a loop stops controlling execution.
Everything between a loop block and its End Block is repeated.
Everything after the End Block runs once.
End Blocks are also used for If blocks, but this guide focuses on loops.
How Scope Works#
Example structure:
Repeat N
→ Click
→ Extract
→ Wait
End Block
→ Next action (runs once)
The End Block ensures loops are predictable, readable, and safe.
1. Repeat N: Fixed Count Loops#
Repeat N is used when you know exactly how many times an action should run.
When to Use#
- Scraping exactly 50 search results
- Sending a set number of messages
- Running a predictable sequence multiple times
How to Set Up#
- Add a
Repeat Nblock to your task. - Fill the count with the number of iterations you need.
- Add actions under the loop — these will repeat each iteration.
- Add an
End Blockafter the last action inside the loop.
Example Use#
- Click a result, extract data, go back, and wait.
- Loop 50 times to scrape 50 search results.
- Use a variable like
indexto track iteration count for naming files or sending to APIs.
2. While: Condition-Based Loops#
While is used when you don’t know in advance how many times a loop should run.
It continues as long as a condition evaluates to true.
You can base the loop on:
- Selector-based conditions (page element exists or changes)
- Variable-based conditions (task logic or state changes)
While Block Fields#
selector→ optional, used for element-based conditionsvalue→ optional, used with selector matchingconditionVar→ variable to evaluateconditionVarType→ string, number, booleanconditionOp→ equals, not equals, contains, exists, etc.conditionValue→ value to compare
Fill either a selector condition or a variable condition, not both.
Example: Click “Load More” Until Gone#
Goal: Load all items on a page by repeatedly clicking a Load More button until it disappears.
How to Set Up#
- Add a
Whileblock- Fill this value with
exists('.load-more') - Leave selector blank because the JS condition handles it
- Fill this value with
- Add a
Clickblock under the While- Fill this selector with
.load-more
- Fill this selector with
- Add a
Waitblock under the Click- Fill this value with
1.5seconds
- Fill this value with
- Add an
End Blockto close the loop
Now the loop will:
- Click the Load More button
- Wait 1.5 seconds
- Repeat as long as
.load-moreexists
Notes#
- Always set a maximum timeout to prevent infinite loops.
- Selector-based While is ideal for UI-driven loops.
- Variable-based While is ideal for logic-driven loops.
3. For Each: Looping Over CSV or Lists#
For Each is used when you want to perform actions on every item in a list or CSV input.
The CSV block supports raw CSV input and also variables for dynamic values.
When to Use#
- Clicking every product link on a page
- Scraping table rows
- Visiting all URLs from a CSV
- Sending each item to an API
How to Set Up a CSV-Based For Each#
-
Add a CSV block
-
Fill this with raw CSV input, for example:
url,name,category
https://site.com/item1,Item 1,Category A
https://site.com/item2,Item 2,Category B -
This CSV will automatically populate variables for each column (
url,name,category).
-
-
Add a
For Eachblock- Fill this with the CSV block output (the list of rows)
-
Add actions under the For Each
- Navigate to
url - Extract details
- Save or send data
- Use variables from CSV like
nameorcategory
- Navigate to
-
Add an
End Blockto close the loop
Each row of the CSV runs once, and variables from that row are available in that iteration.
You can also send each row to APIs like n8n or Zapier inside the loop.
4. End Block: Defining Scope#
Every loop or conditional block must end with an End Block.
- Signals where the loop stops repeating
- Determines which actions are controlled by the loop
- Actions after the End Block execute only once
Example Structure#
- While selector exists
→ Click
→ Wait - End Block
- Next action (runs once)
End Blocks are also used for If conditions, but here they define loop boundaries.
Choosing the Right Loop#
- Known count →
Repeat N - Page or variable condition →
While - Extracted list or CSV →
For Each
Always close loops with an End Block.
Summary#
Control flow turns simple scripts into professional-grade automation agents.
Repeat N→ certainty and predictabilityWhile→ dynamic, condition-based flexibilityFor Each→ structured, list- or CSV-based iterationEnd Block→ scope, clarity, and safety
Master these blocks and you can automate even the most complex, dynamic websites reliably.