How to get the number of elements using 'For Each UiElement

Hello
I would like to know how to get the count of the same element on a page using ‘For Each UiElement.’
For example, if there are multiple instances of ‘yes’ on a page, could you explain how to find out how many ‘yes’ elements there are?
Thank you in advance for your help

Hi @22222222asas,

  1. Use the “For Each UI Element” Activity:
  • This activity is designed to iterate through a list of UI elements. You can set a proper selector that identifies all the elements you are interested in.
  1. Initialize a Counter:
  • Before the loop, you should initialize a counter variable to keep track of the number of elements.
  1. Increment the Counter:
  • Inside the loop, use an Assign activity to increment the counter for each iteration.
  1. Optional: Additional Operations:
  • If needed, perform additional operations on each UI element within the loop.

Sample Workflow

Here’s a simplified example of how you can set this up in UiPath Studio

  1. Create a Variable:
  • Create an integer variable, e.g., elementCount, initialized to 0.
  1. Use “Find Children” or “For Each UI Element”:
  • Use “Find Children” activity (if you want to gather child elements first) and then iterate, or directly use “For Each UI Element” with the appropriate selector.
  1. Inside the For Each Loop:
  • Use an Assign activity to increment the count:
elementCount = elementCount + 1

Cheers.

@dheephiga.am
Thank you for your response
but I would like to know how many elements are present at once. Is there a way to do that?

Alternatively you can try this approach,

For example, if there are 7 “Yes” buttons present on the page or application, using the Find Children activity will correctly identify and return the count of these elements.

Here’s a step-by-step guide on how to set this up in UiPath to count the “Yes” buttons:

Steps:

  1. Create Variables:

    • children: IEnumerable<UiElement>
    • elementCount: Int32
  2. Use “Find Children” Activity:

    • Configure it to locate all “Yes” buttons.
  3. Filter Criteria:

    • Use the Filter property to specify criteria that match the “Yes” buttons (e.g., based on the button’s text or another identifier in its selector).
  4. Assign Count:

    • Use an Assign activity to count the elements.

Example Workflow:

Variables:

  • children (Output of Find Children): IEnumerable<UiElement>
  • elementCount: Int32

Workflow:

  1. Find Children Activity:

    • Selector: Configure the selector to target the parent element containing the “Yes” buttons, or directly target the individual “Yes” buttons if possible.
    • Filter: Example filter string (for buttons with text “Yes” or attribute indicating it’s a “Yes” button):
      "<webctrl aaname='Yes' tag='BUTTON' />"
      
    • Scope: Set to UiPath.Core.Activities.FindScope.Descendants if you need to search deeper in the hierarchy.
  2. Assign Activity:

    • Assign the count of the children to elementCount:
      elementCount = children.Count()
      
  3. (Optional) Log Message / Output for Verification:

    • Add a Log Message activity to confirm the count:
      Log Message
      Message: "Number of 'Yes' buttons found: " + elementCount.ToString()
      

Example Implementation:

Variables:
children: IEnumerable<UiElement>
elementCount: Int32

Sequence:
1. Find Children
   Selector: "<Your Parent Element Selector or general scope>"
   Filter: "<webctrl aaname='Yes' tag='BUTTON' />"
   Scope: UiPath.Core.Activities.FindScopeDescendants
   Output: children
   
2. Assign
   elementCount = children.Count()

3. Log Message
   Message: "Number of 'Yes' buttons found: " + elementCount.ToString()

Additional Resources:

By using this approach, if there are 7 “Yes” buttons present, the count returned by the children.Count() will indeed be 7, accurately reflecting the number of “Yes” buttons on the UI.

@22222222asas

if all elements are similar strcutured then find children also would work and you can get the count

but if you want to get yes from any type of element then better to do a search and get the count from there

cheers

Hi @22222222asas ,
Try to use find children activity


In scope take the required option

Try Using ‘Find Children’ activity if the elements are similarly structured. You can find the count of the number of elements found there. Moreover try utilising the ‘Filter’ option in the activity, returning specific elements lists.
And also to cross check, you can use ‘Highlight Elements’ to reconfirm the number of elements being retrieved.

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