forEach - Selenium IDE
forEach (array variable name, iterator variable name) - Selenium IDE command
The forEach command is used to create a loop that executes the proceeding commands for each item in a given collection. The input is the name of a variable containing a JavaScript array. The iterator variable is simply the name of the variable used when iterating over a collection in a looping control flow command (e.g., for each). In other words, one value of the Javascript array is pushed into this variable for each new loop. It is the same concept as with forEach loops in other programming languages such as PowerShell or Javascript.
To create and fill the Javascript array the ExecuteScript_Sandbox (recommended) or ExecuteScript command is used: ExecuteScript_Sandbox | return ["Hello","Bonjour","你好"]; | myArray
Javascript arrays created this way can not only be used by forEach, but can be used within the Ui.Vision RPA automation script in general. Example: echo | ${myArray[2]} in Chinese means ${myArray[0]} in English.
Break and Continue
If you are working with loops, you might find it useful at some point to either break the loop when a certain condition is met or you might want to skip over one or more iterations of the loop. This is what the break and continue statements are used for.
ForEach Example
The example macro stores 3 text strings in the array A1. Then it uses forEach to loop over each value and prints the current value. Please notice that it is forEach | A1 | greeting with no ${..} around the A1 array variable. The reason for this is that forEach does not take the value of the variable/array as input, but the array itself.
Command | Target | Pattern/Text |
---|---|---|
ExecuteScript_Sandbox | return ["Hello","Bonjour","你好"]; | A1 |
forEach | A1 | greeting |
echo | ${greeting} and have a nice day! | |
end |
Works in
Ui.Vision RPA for Chrome Selenium IDE, Ui.Vision RPA for Firefox Selenium IDE, Firefox IDE Classic
Related Demo Macros
DemoGotoIf, DemocsvSave
The ready-to-import-and-run source code of all demo macros can be found in the Open-Source RPA software Github repository.
See also
while, times, gotoIf, gotoLabel, Label, !statusOK, !errorIgnore, Web Automation Extension User Manual, Selenium IDE commands, Selenium IDE Flow Control (SelBlocks),
Anything wrong or missing on this page? Suggestions?
...then please contact us.