Select Specific String from IEnumerable<UiElement>

I’ve got 99 reasons, but I only want one… :sunglasses:
And they are all in a IEnumerable<> type collection.
I’m not good at Linq, or Lamdas or anything like that.
I am slowly VERY slowly learning that stuff.

So I have a drop down list, that won’t work with select from list activity.
So I had to find the children of the dropdown list,
And sometimes there are lots of them. I don’t want my for each loop
To have to go through Everything. Is there a faster way to Query the IEnumerable List
For a string…

My List = ReasonList
String I’m searching for = adjReason
Item = UiElements in the ReasonList

I have it in a for each loop right now iterating through each child, and I’m saying, for each Item, get the innertext and if it contains adjReason, then continue on to click it.

I’m just wondering if I could use ReasonList.Select(Just the adjustment reason I want. ) Or something like that.

Something like this: https://dotnetfiddle.net/cNRbyb ?

2 Likes

That’s a good example, Thank you.
How do I impliment that into my workflow…

I tried to implement this in my for each loop:

For each Item in ReasonList.Find(Function(x), x.Get(“innertext”).ToString = adjReason)

It’s saying that it’s expecting an expression… So I’m obviously not doing it right…

Got you - you’re expecting more than one hit, sorry - I missed that. In that case you will need to use the FindAll method:
https://dotnetfiddle.net/TQcRDS

FindAll returns a collection that implements IEnumerable, so you can use it in a ForEach. Note that x represents the current iteration (e.g. apple 1, apple 2, and so on).

2 Likes

You’re right, I’m only expecting one hit. I have to examine the innerhtml to see if it matches the adjReason, and if they match I need the Uielement, so that I can click on it.

So I guess I don’t need the for each loop anymore.
I just don’t know how to implement this. Could I assign a variable of Uielement type with whatever is returned from the query?

It all depends on the element itself. For example, if you’re dealing with an option element, the Select activity would work just fine. Simply set a variable to the value you need to select, and then use said variable as an input for the Item property (my screenshot shows a hard-coded item):

select

When dealing with front-end frameworks, things get more complicated as drop-downs might be buttons with elements showing in divs (and some JavaScript doing all the magic). This is where you will need dynamic selectors - i.e. using a variable in the selector itself. Here’s an example - let’s say I need to select “Action” from a Bootstrap drop-down:

The selector for the final click action is then changed to include item, a variable that’s set to “Action”:
"<webctrl aaname='" + item + "' idx='1' tag='A' />"

That’s Entirely what’s happening here. My Drop down list is a Div and each option in the list is also a Div. So It doesn’t work with the “Select” Activity.

So what I have is I have a find children activity, which returns all the reasons in the list to me, What I want to do, (I’m not sure if it’s possible) is use one of these inner functions to return the Uielement in my ReasonsList if it matches the reason I’m looking for.

If I could Pseudo-code it, It would look something like,

ReasonList.Select(Function(reason) reason = adjReason, Return UiElement)

Then if the Element is returned to me, I can use a click activity to click on the correct one.
It looks like all of these functions are returning booleans. I don’t just want to know if it’s in the list. I know that the reason is already in the list. I just want to return the element so I can click on it, without looping through every single reason that was returned from my find children activity.

I don’t think that you need to extract children first. Why not click on the button itself to open the drop-down, and then issue a click on the item you’re looking for (using a dynamic selector)? Here’s an example for a (Bootstrap) drop-down with some elements being off-screen, but still present in the DOM:
https://jsfiddle.net/tagp3mer/

Here’s the robot:
DropDownDynamic.xaml (8.3 KB)

1 Like

I’m getting an error when I do that. The aaname is the “reason” That I’m looking for, so I put my variable into the selector.
adjReason is a string data type.
fruit

Source: System.Xml

Message: Unable to cast object of type ‘Newtonsoft.Json.Linq.JValue’ to type ‘System.String’.

Exception Type: InvalidCastException

Just judging from the error message alone it appears that adjReason isn’t a string (but a JValue). Please make sure that adjReason isn’t just of the Generic Value type, and try again with another variable (use string for its data type).

1 Like

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