How to get the last item of itteration?

Hi there.

I need to get the last element in loop as this is the selector i need to click. Currentyly I got 2 loops - first counts the selectors and second - counts it again and if counter is equal to first counter, then click. This logic works however I don’t like it visualy as it is repeating the same loop :confused: I was wondering if there is another way, more elegant? I’’ve heared about Count(). Might be a solution to run the Count() first and then compare it with counter?

Is the loop running over a list or table , if it is a list you can access with negative indexing, if it is a table you can get count of rows the use datatable.Rows(rowcount)(columncount) eg: for first row dt.Rows(0)(0)

This is web element from find children descendants

I am not sure if I understood you right, but you are looping over an output from find children?

  • retrieving the count
  • looping getting the last

Both can be done within an assign activity

count: YourFindChildrenOutputVariable.Count
Last Item: YourFindChildrenOutputVariable.Last

A particular item: YourFindChildrenOutputVariable.ElementAt(ItemPositionIndexInTheList)

1 Like


Don’t use a loop for this. If you want to click the first item, then use .First, and last item use .Last
You can also use the index, when .First or .Last is not a member of the enumerable.

@ppr gave a good example.

So, don’t use a loop unless you need to perform actions on multiple items. In your case, you can just go straight to the click and use .First or .Last.


Thanks, I will check it out at work :wink:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.