Looping Basics

What follows is a video showing how to implement basic looping in your robots, along with a transcript of the video.

Video transcript:

This video will give you an introduction to looping within your robots. In particular we will be looking at the types of loops which can be accessed directly from the Browser View.

Looping is both touched upon in the Beginner Tutorial Videos and in the video on Branches, Robot States, and Execution Flow. If you have no experience with looping, I highly recommend you to take a look at these videos before proceeding with this video. Take special notice of the way loops alter the execution flow.

The most useful robots are often those which perform a large quantity of actions, simply those which get a lot done. Often this includes performing the same operations in a number of similar cases. An example is the NewsMagazine robot used in the Beginner Tutorials. This robot uses a For Each Tag step to extract text from several blog posts. The For Each Tag step is just one of many loop steps to which the same logic applies. They all somehow let you perform the same procedure in a number of related situations.

The most basic loop steps are categorized as For Each Tag Loops, because they all somehow loop through tags in the current window. Let me go through these basic loops one at a time.

For Each Tag

The first of three loop steps we are going to discuss in this video is the loop literally called For Each Tag.

For Each Tag loops over each tag of a given name directly within the found tag. The first tag in the loop has been indicated by the blue box in this screenshot of the source view. I now overlay the screenshot with lighter blue boxes to show the following iterations of the loop.

For Each Tag is the loop step which I find myself using most often, simply because of its mixture of flexibility and ease of use. It is also the loop step which we used in the Beginner Tutorial Videos.

Whenever I need to loop over a selection of similar tags, like the products listed on this page, the first thing I try is to right click the first element that I want and select Loop >> For Each Tag. Design Studio then sets up a For Each Tag loop which loops through tags similar to the one I right clicked.

I can now iterate through the loop, using the arrows on the For Each Tag step, to view all the named tags formed by the loop. As discussed in the Beginner Tutorial Videos the blue box formed by a loop is called a named tag and is used as a point of orientation for Tag Finders of following steps, so if we insert a step which extracts the price of the first product, the following prices will be extracted likewise in subsequent iterations of the loop. This is the way all loops, considered in this video, operate.

Sometimes, however, it does cause problems to insert the loop, like here on vimeo.com. I right click the first element that I want and choose the For Each Tag loop, but the resulting loop only includes the topmost listed video. We can see this by trying to go to the next iteration. This results in a window opening to tell us that we have reached the last iteration of the loop.

To fix this I have to go directly to the configuration of the loop step where, in this case, I need to remove the specification of class to loop over. Design Studio guessed that we only wanted tags with the class top but really we want to loop over every listed item, independent of class. I delete the class specification and the loop now works as expected.

To use For Each Tag effectively you should study the different ways to configure the For Each Tag step.

For Each Table Row/Column

For the following loops it should be mentioned that they are often interchangeable and a given situation may be handled in any number of ways. I will try to teach you the basic principles of each loop type so you can be intelligent about which type you choose, but there is no single correct way of doing things.

The two next types of loop steps we will look at are both derivatives of the For Each Tag step, but they have more specific uses. They are called For Each Table Row and For Each Table Column and they respectively loop through rows and columns of a table. As with the For Each Tag step they have been conveniently implemented in the right click menu.

In this screenshot the first row is shown with the named tag marked 1 and the first column is the named tag marked 2. As you can see, combining the two types of loops will let you loop over every element in a table.

To insert a loop over table rows or columns, right click on any table element and select the appropriate action from the Loop submenu. You may either choose to include or exclude the first row or column.

I have now inserted a loop which loops through each column of the newest Ikea furniture. Notice that the name of the loop step is For Each Tag. Instead of having a unique step for looping through tables, the For Each Tag step has just been automatically configured to loop through columns in a table.

For Each Tag Path

The next type of For Each Tag loop is called For Each Tag Path. It is very similar to For Each Tag, which we just discussed.

The difference between the two is that For Each Tag Path loops over tags that are at any level inside the found tag whereas For Each Tag only loops over tags that are directly inside the found tag.

Sometimes the tags you want to loop over are not all directly inside one parent tag, or possibly the tags you want to loop over are all on different levels then you will need to use the For Each Tag Path loop. Notice in this example how the div tags looped over are all within a td and a tr tag and are therefore not directly within the found tag.

The easiest way to determine whether to use For Each Tag or For Each Tag Path is to look at the page structure in the Source View.

For Tags with Class

Just like For Each Table Row and Column were derivatives of the For Each Tag loop as is the For Tags with Class a derivative of the For Each Tag Path loop. As an example of the For Each Tag Path loop let me show you how to use this derived version.

Okay, now let us delete the table column loop we set up and take a look at the For Tags with Class. This loop iterates over all tags with the same value of their class attribute, which is often the case for tags with similar content. This time we have to be a bit more specific which tag we select before right clicking and we also have to keep an eye on the source view.

Usually using this loop goes something like this: As we click on the tags containing each product we look in the source view and notice that they all have the same class, namely productContainer. Once we realize this we can simply right click on one of the tags and choose Loops >> For Tags with Class >> productContainer. We then iterate through the loop to check that the named tags match our expectations.

Again notice that the inserted step is not called For Tags with Class but rather For Each Tag Path, which has simply been configured to perform the specific task.

For Each URL

The last loop we will take a look at is the For Each URL action, which is in a category by itself.

For Each URL simply loops through each URL inside the found tag. It is often useful if you need to extract or click on every link in a specific area of a page, regardless of the context of the link.

For Each URL is most easily inserted by selecting the tag containing the links you would like to loop over, then right clicking the selection and choosing Loop >> For Each URL.

I have now set up a loop which iterates through each URL in this article. It by default skips duplicate URLs.

Let's leave the For Each URL action at that. Just note that For Each URL has a number of configuration possibilities which can be changed in the step view.

Finally I have two notes that will help you when using loops.

Note 1

Just to spell out what I said in the video on Branches, Robot States, and Execution Flow: A For Each loop step, like any of those in this video, executes every subsequent step in the robot view for every iteration of the loop, so if you want your robot to continue execution beyond the loop, then you will have to insert a separate branch before the loop step. This branch will then be executed after the loop has finished.

Note 2

It is often nice to be able to break a loop or skip an iteration based on certain conditions. If we for example reach an iteration where one of the steps within the loop cannot be performed, it would often be logical to skip this iteration altogether.

As mentioned in the video on Branches, Robot States, and Execution Flow this can be done by adjusting the Error Handling of the step that fails. In the Step Error Handling View you can choose Next Iteration or Break Loop if an error occurs at this step.

At default this option is set to Skip Following Steps which corresponds to letting the robot hit an end step at its current execution position. In other words, if an error occurs at this step, the robot will go back and execute the next branch of the most recently reached branch point or the next iteration of the most recently reached loop step, however it will also cause an API Exception and Log an Error as indicated by the check boxes.

These were just the basics of looping. To learn more check out the Loops in Forms and Repeat-Next Loop videos. Also feel free to go to help.kapowsoftware.com to read about loops in greater detail.