Uipath For Each UI Element: Source data was modified, for each operation can not continue

Hi All,

I’m using For Each UI Element activity for one of my web portal, my requirement is to remove the UiElement where the the text field like email and contact no is empty, I’m using for each Uielement and able to identify the field which is empty and also able to delete that element as well but when it is going to next iteration its giving me below error
For Each UI Element: Source data was modified, for each operation can not continue.
This is likely due to changes in the target application while the For Each UI Element activity executes.

attached is the sample image to check how I’m removing Uielement by clicking on delete icon
can anyone pls help what to do in this case?

when looping we cannot touch (e.g. delete items) the sequence

One of many options could be to do a filtering with the LINQ Where operator

Create a variable elementList as list(of uielement)

In your For Each instead of deleting the element, add it to the list.

Then after the For Each, loop through the list and do the deletes. That way you’re not modifying the collection that For Each UI Element is looping through, as it’s looping. You’re just making a list of what to delete, and then later going through that list to do the deletes.

Another way would be to use Find Children to get a collection of the elements, then For Each through its results and do the deletes.

any other solution which we can do in for each UiElement activity only?

Other solution besides what? If you want to use For Each UI Element, you’ll have to add each one you want to delete to another variable (a list of UIElement). Then after the FEUIE loop through the list and do the deletes.

I tried this way of creating placed list of uielement and placed it in for each loop but it only deletes/ removes element of first iteration when it comes to second iteration even though the selectors are same and present on the screen still its giving error “Click: The UI element is invalid. Make sure the target application is open and the element is on the screen.”

Try this. Open UI Explorer. Indicate one of the elements. Make sure it validates. Manually delete one of the elements. Then in UI Explorer click Validate again and see what happens.

It sounds like the UI is refreshing when you delete something, so object references change. This means you’ll have to do the Find Children for each iteration of the loop, to re-acquire the object references.