Dynamic Selectors - value containing an & character

Hi,
Has anybody found a way of using an & character as a value in Dynamic Selectors?

I have tried encoding as “&” that gets accepted but generates a selector as “&” which does not match the values.

The requirement is navigate a tree structure that has & characters in both folder names and leaf names.

Hopefully somebody has discovered a solution?

Regards, Dave

Sample_Tree_Structure

I had this issue many times.

To ensure special characters are correctly interpreted in selectors, remember to escape them from the variable before using them. Here are some examples of html special characters: &,",',<,>

Try to use this method, it should escape every special character and after that you can use your variable in selector:

System.Text.Encodings.Web.HtmlEncoder.Default.Encode(yourVariable)

Hi,

Yes, when I posted I tried to show & had become “& amp; amp;” in the dynamic selector value which then fails to match. I had encoded the & character to stop the Dynamic Selector error and get XML substitution by Robot to work.

It seems the value that is being used in the web page is not getting encoded by the Robot but the dynamic selector process that uses XML in the string.Format (blah blah, variable) is encoded. This results in a mis-match.

The error shows as “Could not find the user-interface (UI) element for this action”.

@DavidMartin

If you directly use &amp; it should be working as expected

In the innertext i beleive it would come as above only

Or you can try to use getting the name using idx in the selector and then match the innertext to check if it is matching

Cheers

Hi @DavidMartin -

1)It looks like you’re trying to pass a character “&” as a dynamic selector. And yes, the studio generates a selector as &amp. For each special character mostly, studio creates a different one than the actual one

2)So, when you’re trying to pass “&” into the selector, make sure you change it to &amp and then pass. May be you can maintain these special characters in a config with the below format

Ex: First Column    |     Second Column

        &           |           &amp

3)Before you pass any value as a dynamic selector, refer to the config file. If any special character found in the value, make sure replaces with the respective value. If you come across any other special characters in the future you can easily add to the config, without any code change in your project

Hi,
Thanks for the response.
Yes, I can make an explicit match on any individual selector value but it needs to be dynamic as the process is navigating a folder structure with circa 10,000 entries.

I have already tried encoding the & character as & but that still gives a failure. That is for the aaname not the innertext.

Thanks for the response.

Yes, I appreciate that a config file could be used for the mapping. I could equally store the values in strings as assets in Orchestrator. The way to do the substitution is not the issue.

The issue is getting dynamic selectors to work reliably when the & character needs to be matched.

@DavidMartin

Can you please show the selector that you get for the folder name you want which has &

Also can you please show how you are trying to use in aaname

Cheers

@DavidMartin - My uderstanding was, you’re passing a value with “&” inside the selector (dynamic selector). If this is the case, I guess one of the option would be, replacing the “&” with character(&)amp; before actually passing into the selector. This is also one of the reliable way as everytime you’ll be checking the value has a special character or no. To validate, take IF condition

Value.contains("&")

>> True then take assign activity

Value = Value.Replace("&","&amp;")

>> Else Nothing

Thanks.

Yes, that is the code I have tried. So that the encoded value is passed to the dynamic selector.

It does not reliably match.

The best results I have so far are replacing the & character with a ?, so that is treated as a single character wildcard in the dynamic selector.

This seems wrong to me as I know what it should be matching but it seems UiPath as an issue with matching when characters are encoded.

The target selector is:

string.Format(“<webctrl id=‘folderframe’ tag=‘FRAME’ /><webctrl tag=‘A’ type=‘’ class=‘’ aaname=’ {0} ’ check:innerText=‘{0}’ />”, NodeNameEncoded)

and the anchor selector is:

string.Format(“<webctrl id=‘folderframe’ tag=‘FRAME’ /><webctrl tag=‘A’ type=‘’ class=‘jstree-clicked’ aaname=’ {0}’ check:innerText=‘{0}’ />”, zutecTargetFolderEncoded)

Both of the variables have the & character encoded to & amp;

@DavidMartin

I need the selector when you indicate the element which contains amp…that way we can check the similarities

Orelse try like this zutecTargetFolder.Replace("&","*")

Cheers

@DavidMartin

So I tried this and it is working as expected

this is a dummy button I created

image

This is how selector looks

image

This is the input and how I formatted selector and it is working as expected and clicking on it
string.Format("<webctrl tag='A' aaname='{0}' />", str.REPLACE("&","&amp;"))

and the variable value

Hope this helps

cheers

Hi @DavidMartin

Instead of encoding the ampersand as “&”, try using the HTML encoding “&”. This should ensure that the selector is properly interpreted and matches the desired elements containing the ampersand character.

If the above solutions don’t work, you can try escaping the ampersand character by using double ampersands (“&&”) in the selector. This can help bypass the encoding and ensure that the selector is correctly interpreted.

Thanks!!

Hi Anil,

That is the selector, with dynamic elements.

In both cases the & character has been replaced. I have used * to get around the encoding issue, the best results are using ? as it is only a single character widlcard so the accuracy of the rest of the match is better.

Thanks. That is effectively the code I already have in place.
The only difference being your selector is using aaname, whereas I have the selectot using aaname and check:innerText

I don’t believe entering the & character twice is “escaping”. Not sure if something got encoded in your reply.

@DavidMartin

Agreed with ? Results as well…I invluded star just in case if there are single results…

And having aaname or in ertext should siffice as having both is not helping or making it more unique in anyways…so we can remove either one

I hope with this you are able to valudate and use the selector now

Cheers

I believe aaname and check:innerText could have different values, it just depends on how the page has been constructed.

I am expecting them to be the same in the process. By giving UiPath more elements to match on, it should improve the result not detract from it.

Thanks for your help. I will continue to experiment.

1 Like

@DavidMartin

Just to clarify…

aaname most or almost always is same with innertext…from the script side there is no control on aaname field…its an auto generated…

Its not bad to give both…but as they are same it is not helping explicitly in identifying in anyway…so it is more redundant

And I believe Replacing & has been working…if not would really be happy to check what is different in the site you are using as it has been working that way. And that is the same reason I asked for a selector containing the actual aaname or innettext instead of the formatted selector after adding dynamic variables

cheers