Skip to main content

Nested Loops in RPA: Handling Multi‑Layered Structures

Sophie avatar
Written by Sophie
Updated this week

Real-world data often comes in layers. Think of a news site with multiple pages, and each page containing multiple articles. To capture everything, you need a pattern that automatically “visits every page and processes every item on each page.” Nested loops are the foundation for this: one loop controls the pages, while another loop handles the items on each page.

Core Concept: A Loop Inside a Loop

Imagine you’re reading a book: turning each page is the outer loop, and on each page, reading every paragraph is the inner loop. You don’t turn the page until you’ve finished every paragraph on the current one. If you can visualize this, you’ve already mastered the logic of nested loops!

Technically, a nested loop places a complete "inner" loop inside the body of an "outer" loop. Each time the outer loop runs once, the inner loop completes all of its iterations. This “whole-to-part” structure keeps your automation organized and predictable when dealing with multi-level data.

A Practical Scenario: Paginated Product Listings

Let’s apply this to a common e-commerce task: scraping product titles across 5 pages.

The Blueprint:

  • Outer Loop (Pagination Control): Use Loop by Count to define how many pages to process (e.g., 5 times).

  • Inner Loop (Item Handling): Inside the outer loop’s body, use Loop through similar web elements to find all product cards on the current page.

  • Extraction Action: Inside the inner loop, add steps to extract the "Title" and "Price."

  • Navigation Action: After the inner loop finishes—but still inside the outer loop—add a "Click Next Page" step.

How the Flow Runs:

  1. The robot enters the Outer Loop (Page 1).

  2. It triggers the Inner Loop: iterating through all 20 products on Page 1.

  3. Once the 20th product is done, the inner loop ends.

  4. The bot executes the "Click Next Page" step.

  5. The Outer Loop starts its second iteration (Page 2), and the cycle repeats.

Design Essentials and Reliable Patterns

To build nested loops like a pro, keep these three rules in mind:

A. Mind the "Next Page" Placement

The most common mistake is placing the "Next Page" click inside the inner loop. This would cause the bot to turn the page after every single item. Rule: Ensure the pagination click happens only after the inner loop has processed all items on the current screen.

B. Anchor Your Selectors (Relative Positioning)

All actions inside the inner loop must be Relative to the currentItem. If your extraction step uses an absolute path, it might keep grabbing data from the first product on the page instead of moving down the list.

C. Stability and Timing

Nested loops multiply the number of actions (e.g., 5 pages × 20 items = 100 extractions). To prevent crashes:

  • Add a short Wait after clicking "Next Page" to ensure the new product grid has fully loaded before the inner loop starts again.

  • Use Error Handling to skip a single item if it’s missing a price, rather than stopping the entire 5-page process.

Summary: From Simple Scripts to Robust Solutions

Nested loops allow you to decompose a large, overwhelming task into smaller, manageable units. By coordinating an outer "navigator" with an inner "worker," you can automate almost any complex web structure with confidence.

What’s Next? Now that you can handle multi-page data, you’ve completed the core Loop Logic series! Ready to see how these loops interact with Conditional Logic (If/Else) to handle unexpected pop-ups or "Out of Stock" items?

Did this answer your question?