Parallel for each with UiPath.Core.UiElement

I’m using a find children activity and then a for each to evaluate and add some of them to a DataTable.
The evaluation has string manipulations and a few Get Attributes activity to fetch the page based on the actual element. As this for each seemed pretty simple I tried to go with the parallel for each but it throws this:

Catched error UiPath.Core.ElementOperationException: Error HRESULT E_FAIL has been returned from a call to a COM component. ---> System.Runtime.InteropServices.COMException: Error HRESULT E_FAIL has been returned from a call to a COM component.
   at UiPath.UiNodeClass.Get(String bstrAttr)
   at UiPath.Core.UiElement.Get(String attribute, Boolean refresh)
   --- End of inner exception stack trace ---
   at UiPath.Core.Activities.InvokeWorkflowFile.EndExecute(AsyncCodeActivityContext context, IAsyncResult result)
   at System.Activities.AsyncCodeActivity.System.Activities.IAsyncCodeActivity.FinishExecution(AsyncCodeActivityContext context, IAsyncResult result)
   at System.Activities.AsyncCodeActivity.CompleteAsyncCodeActivityData.CompleteAsyncCodeActivityWorkItem.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)

Does this mean that the Get Attribute activity can not be used inside a parallel for each loop?

What would be the workaround to save the page into a variable and process that as the Get Attribute activity but not using actually the live browser? If I’m guessing right that this is causing the exception.

Try in the form of VB expression inside Parallel foreach instead of activity and see if you are still getting the error.

strVal = item.Get("aaname").ToString

The get item activity has a selector to get an element out of a webpage as in my example. I need the activity because of this selector that is dynamic based on the actual children element in a for each loop. Hope it is clearer now.

Vaguely understood. We are just trying to do what the activity is doing in the background.

strVal = item.Get("aaname").ToString

Here item is the children from your foreach.

item.Selector.ToString will get you the Selector of the Children.

In the case you are suggesting the child element would be selected to get its attribute and not my dynamic selector would yield the element in which’s attribute I’m interested in. The element for which I want to get its attributes is not the child element I have as item but the selector gets it as it is composed based on item’s properties (like TableRow, and want to get something in the same row from page).

I think the parallel for each fails because I can not fetch a live element and that is what is happening when I give only a selector for a Get Attributes activity’s Target. So now I select the whole page with a find element and afterwise just provide that with my dynamic selector using a VB expression:

BaseUiElement.FindFirst(FindScope.FIND_DESCENDANTS, new Selector("<webctrl tag='INPUT' colName='something' tableRow='"+itemRow+"' type='checkbox' />"))

Instead of using the attach window’s element with the selector now I’m giving this as the Target element and it does work in simple for each mode. When I try it in parallel, it yields the same error. And interestingly with this element approach it seems like the process slows down. With just selectors it took around 17 seconds, now around 20 to finish.

Question : If you are finding an element within a Table and you want static data, can’t you datascrape it to a Datatable and iterate it?

BaseUiElement.FindFirst(FindScope.FIND_DESCENDANTS, new Selector("&lt;webctrl tag='INPUT' colName='something' tableRow='"+itemRow+"' type='checkbox' /&gt;"))

Or is it the checkbox value in a cell that you are trying to get? Can you share the screenshot of what you are trying to do within the Parallel Foreach? Also, did you try this on any other website to see if there is any difference?

Yes, the table’s cell elements have checkboxes or anchors with href values that I get with as UiElements with selectors, so I prefer to have the UiElements instead of the dataTabe.

Success. The parallel for each works if I replace all the Get Attribute into two assigns, first to get the UI element with selector:

BaseUiElement.FindFirst(FindScope.FIND_DESCENDANTS, new Selector("&amp;lt;webctrl tag='INPUT' colName='something' tableRow='"+itemRow+"' type='checkbox' /&amp;gt;"))

And then get its attribute by

strVal = item.Get("aaname").ToString

Still I have the feeling that working with plain selectors was faster then passing UiElements around.

Probably because I work now only with a small set of data the simple for each performs the actions in the same time as the parallel for each does… so unless there are really large sets of data there is no point in switching to the parallel for each loop.

Thank you @vvaidya for assisting on the VB expression way of working with UiElements it did help to get me in the right direction.

1 Like

Agreed. Sometimes the Parallel Foreach may be slower due to the overheads involved.

Just a thought: The parallel feature would be more useful in a parallel reframework statemachine, doing the processing in parallel. If I do the heavy lifting to enable the processing in paralell that could be benefited only from the long run. Would that be possible, is there such a fork of the framework?

Hello @obeliksz and @vvaidya,

I am really interested in your conversation and I have some questions about it.

I am working on a parallel for each activity where I invoke a workflow using the following activities :

  • Open browser
  • Attach browser
  • TypeInto
  • Click
  • OnUiElementVanish (activity which corresponds to long delay in my case and justifies the use of Parallel For Each)
  • Multiple GetValue activities ( = Get Text)

For all the activities in bold I use a selector and an informative screenshot.
Should I remove all the screenshots to make it stable ?
Should I replace all these activities by Assign ones?

Since web pages are changing through the invoked-workflow steps, I can’t use page as a variable, that is why I use attach browser and UiElement-related activities…
What would be the best practices in that case to make sure it is stable ?


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