How to use for loops in Robot Framework and Python
For loops can be used to repeat an action based on a list of items. For example, send emails to a list of recipients. Or process each line of an invoice.
View the following video for Robot Framework and Python for loop examples!
In addition to having nice Robot Framework documentation, such as how to use if-else or conditional execution in Robot Framework we provide tools that make Robot Framework development easier!
๐ Automation Studio
๐ VS Code extensions
๐ RCC command-line tool for robot development.
A simple for loop in Robot Framework
Let's go over it line by line:
Here we are creating a list variable, and storing our robot names in it.
Note that list variables use the
@{}
syntax. You can learn more about variable types on the Robot Framework official documentation.
Here we are starting our for loop. We will loop over our @{ROBOTS}
list variable, and we are defining a ${robot}
local variable, which will be assigned at each iteration.
This is the operation we want to execute at each iteration. In our case, we just want to log the variable's value, so we use the Log
keyword. Note the indentation: this and any other keywords we want to execute inside our for loop will need to be indented with at least two spaces.
To signal that we want to close our for loop, we add the END
keyword.
Our code will now print the name of each of the robots in sequence:
Controlling the execution of a for loop manually
A for loop will execute the same operation for all the elements of a list. Sometimes you might instead want to skip some element or to halt the for loop immediately based on some condition. Let's see how.
Skipping a for loop iteration using CONTINUE
If you want to log the name of each robot in our list, but not Terminator
, you can use CONTINUE
:
- Using an
IF
condition andCONTINUE
, we can skip the current execution of the for loop and continue to the next one if the condition is met.
Breaking out of the for loop using BREAK
If you want to stop the execution of the for loop based on a condition and have your program continue onwards, you can use BREAK
:
- Using an
IF
condition andBREAK
, we can stop the execution of the for loop and have our program continue after it.
For loop examples in Robot Framework
Other practical examples and courses
- In our Beginners' course we use looping to fill a form repeatedly using data from an Excel file.
- The RPA form challenge has a similar use case filling forms.
- The Web store order robot uses looping to place a list of orders in a webshop.
- The PDF printer robot creates PDF files starting from a list of attendees for an event using a for loop.
- The Web scraper robot iterates over the last tweets for a given user with a loop.
- Sometimes a while loop can be more suitable than a for loop.
- Use the Robot Framework cheat sheet and quick reference as your daily Robot Framework syntax reference.
Further reading
In this article, we have seen the most common use cases for loops in Robot Framework. If you want to learn more, you can refer to the official Robot Framework User Guide.