# Using Loop in Leapwork

## Introduction

Like any other programming language in Leapwork, a Loop block is used to perform a certain operation(s) for a specific number of times or until a condition is true.

It can be used in two ways:

1. By changing the 'Use Occur' property of the respective building block to 'All'.
2. Using the 'Loop' building block explicitly.

## By changing the 'Use Occur' property of the respective building block to 'All'

Users can change the 'Use Occur' property to 'All' for the supporting building blocks in Web Automation, Desktop UI, and Image and Text use cases. This means that Leapwork will execute the respective block for the defined number of times until the condition inside is TRUE. For example, it has read all of the values inside the below drop-down.

## Script

As shown above, first we need to make a dynamic strategy where it identifies all the elements, then set the 'Use Occur' property to 'All' in order to find all the elements and read related text using Get Web Text block.

## Use Occurrence

Select which occurrence of the web element to use, if more than one is found.

Select “All” to iterate through all of the occurrences. By selecting “All”, the sub-properties ‘Current index’ and ‘Completed’ are shown (see below).

## Current Index

The current index when iterating through all occurrences of the web element. For instance, if three occurrences of a _div_ tag are found, this property will contain 1 for the first one, then 2 for the second, and finally 3 for the last one.

## Output

The green output connector triggers when the iterations of all occurrences are completed.

When the above script is executed, the result is as shown below.

### Blocks that support the 'Use Occur' feature are:

|     |     |     |     |
| --- | --- | --- | --- |
| **Web Blocks** | **Desktop UI** | **Find And Get** |
| Click Web Element | Click UI Element | Find Image |
| Find Web Element | Collapse UI Element | Find Text |
| Use Browser Window | Expand UI Element | Select Text |
| Get Web Dropdown | Find UI Element | Get Text |
| Web File Upload | Get UI Number | Get Number |
| Hover Web Element | Get UI Selection | **Mouse And Keyboard** |
| Set Web Dropdown | Get UI Text | Click Image |
| **Data-Driven & Logic** | Invoke UI Element | Click Text |
| Read Excel | Select UI Element | Hover Text |
| Database | Set UI Element | Hover Image |
| Regular expression | Toggle UI Element |  |
|  | Use UI Window |  |

## Using the Loop Block Explicitly

Loop functionality can be used externally with the help of the Loop building block.

For instance, the above example of selecting the drop-down menu items can be achieved using the Loop block as well.

**Note**: The current value of the Loop block is passed to the Use Occur property of Find Web Element in the Strategy Editor to find the menu items at runtime.

## Strategy Editor for Find Web Element

****

## Runtime

****

For any clarification, contact our [Priority Support](mailto:prioritysupport@leapwork.com "mailto:prioritysupport@leapwork.com").
