In our previous guides, we learned how to drive loops using data lists or fixed counts. However, sometimes the data you need isn't in a spreadsheet, it's scattered across a webpage in a repeating pattern, like a list of search results or a row of buttons.
This is where Looping through Similar Elements becomes your most powerful tool. Instead of preparing data beforehand, you teach your Bot to recognize a visual pattern and handle every matching item it finds on the screen.
Concept: Data-Driven vs. Pattern-Driven
To master this advanced loop, you must understand how it differs from a standard List Loop:
List Loop (The "Menu"): You provide a predefined list (e.g., an Excel file). The bot follows your "menu" one by one.
Similar Elements Loop (The "Pattern"): You show the bot one "sample" on the screen (e.g., one product card). The bot then scans the page to find all "look-alikes" based on their HTML structure or UI attributes.
The Core Engine: The instruction used is Loop through similar web elements (or the desktop equivalent). It transforms the user interface itself into a dynamic data source.
When to Use It: Batch Processing UI Structures
If you can point to one item and say, "There are many others just like this," you have a candidate for this loop. Typical scenarios include:
E-commerce: Clicking every product link on a search result page.
Data Tables: Extracting information from every row in a web table.
Management Consoles: Toggling a series of status buttons or checkboxes in a list.
How It Works: From Sample to Execution
The process moves through three automated stages:
Automatic Identification: You capture one example element. Octoparse AI analyzes its "DNA" (selectors, tags, and hierarchy) to find all matching peers.
Automatic Traversal: The bot creates a hidden collection of these matches and prepares to visit them in order.
Repetitive Execution: For every "Current Item" found, the bot executes the Loop Body you’ve designed.
The Golden Rule: Relative Actions
This is the most critical logic for intermediate developers. When you are inside the loop body, your actions must be Relative to the current item.
The Mistake: Telling the bot to "Click the 'Buy' button on the page." The bot will likely keep clicking the first button it finds, over and over.
The Professional Way: Telling the bot to "Find and click the 'Buy' button inside the current element."
By keeping your actions relative, your automation remains stable even if the website layout shifts or some items are missing specific details.
📕 Deepen Your Technical Skills
To apply the "Relative Action" rule correctly, you need to know how to set up your selectors. Check out our detailed guide on Absolute vs. Relative XPath to master the technical side of element locating.
Practical Implementation Checklist
Before you run your loop at scale, follow these stability steps:
Capture Thoughtfully: Choose a "sample" that represents the whole unit (e.g., the entire product container, not just the price text).
Validate the Matches: Use the "Check" or "Recapture" feature to ensure the bot hasn't picked up unrelated items from the sidebar or footer.
Handle Dynamic Loading: If the page uses "infinite scroll," add a scroll action inside or before your loop to ensure all similar elements are rendered and visible.
Summary
Looping through similar elements shifts your RPA strategy from processing data to navigating patterns. It scales your impact effortlessly: once you've defined the logic for one card or row, you've defined it for hundreds.
Next Step: Now that you've mastered the three core loop types (List, Count, and Similar Elements), would you like to learn how to Nest Loops—for example, looping through a list of categories and then looping through similar products within each?
