Nested Loops - Handle Pagination
Last updated
Last updated
You can use another Start Repeat building block inside of your loop. This is called nested loop (loop inside a loop). You can create as many nested loops within your main or other nested loops as you wish.
Its primary use case is to handle pagination.
Imagine you want to save a list of profiles from LinkedIn search results. LinkedIn usually shows 10 profiles per page. This is why you would need to set up a Start Repeat building block with a repetition number of 10. See example set-up here: Save Lists.
But how do you make your TaskBot not just save 10 results from one page but go through, for example, 30 pages of results (making it a total of 300 collected profile links)?
For this or any other paginated results, you need a nested loop.
Here is the workflow set-up:
Open Link opens the page where the list of profiles is displayed.
Your main loop needs to loop through 30 pages.
Your nested loop will loop through profiles and save them, 10 per page.
After your nested loop finishes saving 10 profiles, main loop continues via After Repeat with the action Click. Action click will click on the button "Next" and thus paginate to the next page.
Let's summarize the learnings:
Loop is set up to repeat 30 times. This corresponds to the number of pages with search results on LinkedIn.
Within every repetition, your TaskBot enters the nested loop and saves 10 profiles.
After the nested loop is completed, the TaskBot continues (via After Repeat) with clicking "Next" button to go to the next page. Note that After Repeat must be placed after the nested Start Repeat block.
After that, the cycle repeats: Save 10 profiles, then click on "Next" - for 30 times.