Repeat-Next

Click to open a video demo introducing the Repeat-Next loop.

This video will give insight into an advanced but very useful loop called a Repeat-Next loop, which works particularly well for looping through pages where each page leads to the next.

We will build upon the skills learned in the Looping Basics video, so make sure to watch that first.

Repeat-Next

This loop type is the oddball in the looping family, or maybe you could argue that it is not even part of the family because the way this loop is designed is totally different from all the other loop steps in Design Studio.

First of all, the Repeat Next loop consists of two individual steps which need to be used collaboratively to have any kind of effect.

The concept is actually pretty simple: you place a Repeat step followed at some point by a Next step. When execution reaches the Next step, execution will revert back to the Repeat step and proceed execution from there, marking one iteration of the loop. If a Next step is not reached after the Repeat step, then the loop will terminate.

The catch here, and also what makes the Repeat-Next loop so genius, is that while most of the robot state is reverted at the beginning of each iteration, the page reached at the Next step is actually transferred to the next iteration of the loop. So, unlike the other loops we have looked at where the entire robot state is reverted at the beginning of each iteration, we can handle a different page in each iteration of the Repeat-Next loop.

Demo

A typical example of using the Repeat Next loop is when looping through multiple pages of search results. We insert a Repeat step … followed by a step which clicks to get to the next page … and then a Next step … It is as simple as that to loop through all the pages. We can now use the arrows on the Repeat step to iterate through the loop and observe that a new page is loaded for each iteration.

Of course, we still need a way to terminate the loop. This can be done by setting the error handling of the Click step to "break loop". If the click step does not find a link to the next page, we assume that we must have reached the last page and the loop breaks.

If we then want to perform some steps on each page, we can add a branch step after the Repeat step … and add a new top branch. In this new branch, we can add loops and other steps without them being influenced by the Click and Next actions in the other branch. I can, for example, add a loop over all the search results on each page … extract information from each result … and return that collected information. In total, I get a robot which extracts every result from every page of the search. We can get an idea about the execution flow by single-stepping through the robot in Debug Mode. I have sped up the recording so you can clearly see how all search results are extracted, one page at a time.

Remember the form in which the Repeat-Next loop is used here. A top branch which executes the steps you want to perform on each page and a lower branch which loads the next page and calls the next iteration of the loop. This constellation is very useful and very common when using this type of loop.

Note that setting the error handling of a step to "Next Iteration" does not work with Repeat-Next loops. In order to proceed to the next iteration, a Next step must have been executed.