For Each UI Element - Index Out of Range

I am using the For Each UI Element from the Modern Design Experience experiencing Index out of Range issues. The For Each had a Strict Selector which validates, and the UI Elements collection returns values in the Preview, however it continually generates the Index Out of Range error.

Very curious what I’m doing wrong with this and if anyone else has experienced issues with this control?

Happy to provide any additional details you think would be relevant. I’ve included the Properties panel for the ForEach and the Selector which (to me) shows it identifying correctly.

And the error message:

23.4.1+Branch.master.Sha.43c76fe6d960fdc239be3af93d0fd47b8ca81224

Source: Retry Scope (Retry Scope)

Message: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')

Exception Type: System.ArgumentOutOfRangeException

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')   at System.Activities.Statements.Throw.Execute(CodeActivityContext context)
   at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
   at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)
--- End of stack trace from previous location ---
   at UiPath.UIAutomationNext.Activities.NApplicationCard.OnFault(NativeActivityFaultContext faultContext, Exception propagatedException, ActivityInstance propagatedFrom)
   at System.Activities.Runtime.FaultCallbackWrapper.Invoke(NativeActivityFaultContext faultContext, Exception propagatedException, ActivityInstance propagatedFrom)
   at System.Activities.Runtime.FaultCallbackWrapper.FaultWorkItem.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)

To troubleshoot this issue, here are a few things you can check:

  1. Verify the selector: Double-check the selector used in the “For Each” activity’s Strict Selector property. Ensure that it correctly identifies the UI elements you want to iterate over.
  2. Check the UI elements collection: Ensure that the UI Elements collection you’re providing as input to the “For Each” activity contains the expected elements. It’s possible that the collection is empty or doesn’t contain the elements you’re trying to access.
  3. Validate dynamic changes: If the UI elements are dynamically changing during runtime, make sure that the collection is updated accordingly. If the elements are added or removed while the automation is running, it can lead to index out of range errors.
  4. Consider synchronization delays: If the UI elements take some time to load or become available after a certain action, there might be a synchronization issue. You can try adding a delay or waiting for a specific condition before executing the “For Each” activity.
  5. Debug mode: Run your automation in debug mode to step through each iteration of the “For Each” activity. This can help you identify at which index the error occurs and investigate why it’s happening.

troubleshooting:

  1. Verify Strict Selector: confirmed
  2. Confirmed the UI elements collected: confirmed collection contains elements
  3. Validate dynamic changes: array of elements does not change during execution. (Note, there have been instances where the array does change during execution, but when troubleshooting today, this has not been the case.)
  4. Consider synchronization: I have Wait for Page load - Complete, as well as a 5 second delay before beginning the For Each activity, so the page should be completely loaded at that time. (Also, when watching the webpage HTML during the setup process, I have not noticed the setup changing after finishing setup to beginning the For Each process.)
  5. Run in Debug: I’m getting the same “For Each UI Element: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter ‘index’)”. I understand this is the issue, but I don’t understand why this is the issue…

I gave up and switched to a Do While loop that implements GetAttribute and a Parameterized strict selector for the Index (each row has a unique rowID), and Find Descendants to get the Element I’m looking for in each Row.

The new process works, so all good.

Hi @Adam_Eagle

image

Looks like your First Element is not returning all the items in the webpage, could you check and map this element again, because you are getting this error when your reach the null value, if you can’t retrieve all the elements with for each ui element, then you should consider to make a dynamic click instead this activity

Regards!

Hi @Adam_Eagle -

  1. Is the activity not working for a single ui element ?
  2. Another observation, the activity not identified the elements with 0 value
  3. You’ve modified the Timeout value to 3 sec. Can you make it default and check how that goes