Escape last iteration while using for each loop for find children item

  1. I am using find children activity to get child item and getting child item.
  2. lets say I get 10 ui element.
  3. Now using for each loop, iterating through each ui element and using get attribute of ui element( ui element of point no. 2)
    however I don’t want to get attribute of 10th item, how to eliminated that i.e. how get attribute of only 9 item.
1 Like

@gupta.rak1984 using if loop check whether current element in loop is last element of the array. If condition is true then do nothing otherwise do necessary operations.

To find current item index in array method will be something like below. Check method once in forum or Google once.
Array.indexOf(arrayName,loop variable) or Array.indexOf(loop variable, arrayName)

@gupta.rak1984

an alternate approach could be to limit the list to nine elements in advance with the help of an Take Statement: https://www.tutorialsteacher.com/linq/linq-partitioning-operators-take-takewhile

For the calculation of which nth element the loop is running the For Each Activity index can be used as well:
https://docs.uipath.com/activities/docs/for-each

Output

  • Index - A zero-based index that specifies which element of the current collection is being iterated, stored in an Int32 variable.

Stopping the for each loop is done with the Break Activity:
https://docs.uipath.com/activities/docs/break

Aah
Again you got the answer in your query itself buddy
“BREAK”
Yes these are steps to be used
—inside the for each loop at last mention use a assign activity like this
counter = counter + 1 Where counter is a variable of type int32 with default value as 0 defined in the variable panel
—next to this assign use a IF activity like this
counter = 8
If this is true it will get into THEN part where we can use a BREAK activity so that once after reaching 9 element it will get out of for each loop

That’s all you are done
Kindly try this and let know for any queries or clarification
Cheers @gupta.rak1984