Note: You are looking at a static snapshot of documentation related to Robot Framework automations. The most recent documentation is at https://robocorp.com/docs

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!
👉 VS Code extensions
👉 RCC command-line tool for robot development.

A simple for loop in Robot Framework

*** Settings *** Documentation A simple for loop example. *** Variables *** @{ROBOTS}= Bender Johnny5 Terminator Robocop *** Tasks *** Loop over a list of items and log each of them FOR ${robot} IN @{ROBOTS} Log ${robot} END

Let's go over it line by line:

*** Variables *** @{ROBOTS}= Bender Johnny5 Terminator Robocop

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.

FOR ${robot} IN @{ROBOTS}

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.

Log ${robot}

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.

END ${robot}

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:

Log result of for loop

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:

*** Settings *** Documentation Execute a for loop but skip one element. *** Variables *** @{ROBOTS}= Bender Johnny5 Terminator Robocop *** Tasks *** Execute a for loop only three times FOR ${robot} IN @{ROBOTS} IF $robot == 'Terminator' CONTINUE Log ${robot} END
  • Using an IF condition and CONTINUE, 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:

*** Settings *** Documentation Break out of the for loop on condition. *** Variables *** @{ROBOTS}= Bender Johnny5 Terminator Robocop *** Tasks *** Break out of the for loop on condition FOR ${robot} IN @{ROBOTS} IF $robot == 'Johnny5' BREAK Log ${robot} END
  • Using an IF condition and BREAK, we can stop the execution of the for loop and have our program continue after it.

For loop examples in Robot Framework

*** Variables *** ${STRING}= cat ${NUMBER}= ${1} @{LIST}= one two three &{DICTIONARY}= string=${STRING} number=${NUMBER} list=@{LIST} *** Tasks *** Loop a list Log ${LIST} # ['one', 'two', 'three'] FOR ${item} IN @{LIST} Log ${item} # one, two, three END FOR ${item} IN one two three Log ${item} # one, two, three END Loop a dictionary Log ${DICTIONARY} # {'string': 'cat', 'number': 1, 'list': ['one', 'two', 'three']} FOR ${key_value_tuple} IN &{DICTIONARY} Log ${key_value_tuple} # ('string', 'cat'), ('number', 1), ('list', ['one', 'two', 'three']) END FOR ${key} IN @{DICTIONARY} Log ${key}=${DICTIONARY}[${key}] # string=cat, number=1, list=['one', 'two', 'three'] END Loop a range from 0 to end index FOR ${index} IN RANGE 10 Log ${index} # 0-9 END Loop a range from start to end index FOR ${index} IN RANGE 1 10 Log ${index} # 1-9 END Loop a range from start to end index with steps FOR ${index} IN RANGE 0 10 2 Log ${index} # 0, 2, 4, 6, 8 END Nest loops @{alphabets}= Create List a b c Log ${alphabets} # ['a', 'b', 'c'] @{numbers}= Create List ${1} ${2} ${3} Log ${numbers} # [1, 2, 3] FOR ${alphabet} IN @{alphabets} FOR ${number} IN @{numbers} Log ${alphabet}${number} # a1, a2, a3, b1, b2, b3, c1, c2, c3 END END Exit a loop on condition FOR ${i} IN RANGE 5 IF ${i} == 2 BREAK Log ${i} # 0, 1 END Continue a loop from the next iteration on condition FOR ${i} IN RANGE 3 IF ${i} == 1 CONTINUE Log ${i} # 0, 2 END

Other practical examples and courses

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.

Last edit: May 5, 2022