Control.Loops.For
Description
The For Each UI Element activity is common For each Loop that expects to loop over an array of UIElements. The advantage over a for each loop is simply that you don’t have to select the correct variable type (UiElement) you want to iterate over in the beginning when you drag the activity into the workflow.
Properties
Analyst
- Description: Optional text for documentation purposes.
Common
- Disable Protocol: Disables the protocol function for this activity.
- Disable Log: Disables the logging function for this activity.
Input
- UiElementCollection: Input of the activity is an array of UIElements (UiElement[]). Usually, one first uses the Get all UI Elements activity to retrieve all relevant UIElements in the application or browser and then works with the For Each UI Element Activity to loop over this array of elements. Hence, the output of the Get All UI Elements Activity serves as input for the For Each UI Element Activity.
Example
Suppose you want to print the article links under the headline Release History on our Helpdesk site. If we further assume that the order of links will change in the future (Hyland RPA 2025.1 etc.), it makes sense to select all menu items shown under the sub-menu Release History.
The first part of the example (retrieving the UIElements) can be found here. For now, you can assume that we have already an array of UIElements with all the article links under the headline Release History.
Within the loop, you can use the Get Text activity to retrieve the displayed text of each link. The only difference is that instead of using the property Selector Value, the UIElement you are currently iterating over can be set directly in the property Element.
After the workflow is run, all link titles under Release history are printed to the output log:
Tips and Tricks
- Make sure that you are familiar with activities that are commonly used in combination with For Each UI Element like the below-mentioned activities.
- You can use the For each Loop with Index and variable type UiElement if you want to use an index within your loop.
Troubleshooting
-
InvalidOperationException: Collection was modified; enumeration operation might not execute.
If you try to manipulate the array of UiElements you are currently iterating over, this exception will be thrown. In general, a collection that serves as iterator cannot be modified at runtime of the For each Loop. You can, however, safely add or remove UiElements using a While loop. For a generic example, see Add Table Row. -
NullReferenceException: Object reference not set to an instance of an object.
Your array of UIElements (UiElement[]) is null, probably because it was not initialized correctly. Check the point in your process where you initialize this variable (for example, the Get All UI Elements Activity) and verify that you set the correct variable as output.
See also