cancel
Showing results for 
Search instead for 
Did you mean: 

Using Regex in the attribute of an element in Application Modeller

IvanKhudur
Level 4
Hi there,

I am trying to make an action which should check if a specific label in a list of lables on a webpage exists, through its Value and HTML Path. 
The Value has match type dynamic, i.e. it is an input to the action and the HTML Path changes depending on the labels position in the list and whether or not the label is clickable.

Instead of having an element for each label in the list, I want to make the attribute HTML Path have the match type Regex with the regex \d{1,2}. As an example: /HTML/BODY(1)/ should be /HTML/BODY(\d{1,2})/ in Application Modeller.

This way I will be able to find a specific lable through its Value using only 1 element in Application Modeller instead of  >20 unique elements

However, this does not work. I have tried making HTML Path Dynamic, and in my Wait Stage made the HTML Path params be equal to regex, but I get an error message there as well.

My question is: How do you use the Regex match type inside of Application Modeller?

Kind regards,
Ivan

------------------------------
Ivan Khudur
------------------------------
1 BEST ANSWER

Best Answers

IvanKhudur
Level 4
In case someone comes across this and wonders how to solve it:

An escape character (i.e. "\") is needed before each special character:

The string I wrote in my first post /HTML/BODY(\d{1,2})/ should instead be written as \/HTML\/BODY\(\d{1,2}\)\/


//Ivan


------------------------------
Ivan Khudur
------------------------------

View answer in original post

8 REPLIES 8

IvanKhudur
Level 4
In case someone comes across this and wonders how to solve it:

An escape character (i.e. "\") is needed before each special character:

The string I wrote in my first post /HTML/BODY(\d{1,2})/ should instead be written as \/HTML\/BODY\(\d{1,2}\)\/


//Ivan


------------------------------
Ivan Khudur
------------------------------

HI Ivan,
I tried the same but its's throwing expression for me. I set properly asa Dynamic and while passing I passed value as Regx as you mentioned but it did not work.

Could you please help me with that.

------------------------------
Amlan Sahoo
RPA Consultant
Equinix
------------------------------
Regards,
Amlan Sahoo

Hi Amlan,

Could you please show how your regex looks like, as well as the exception?

Kind regards,
Ivan

------------------------------
Ivan Khudur
------------------------------

Hi Ivan,

I just copied the same path you have provided above and tried to highlight.

------------------------------
Amlan Sahoo
RPA Consultant
Equinix
------------------------------
Regards,
Amlan Sahoo

Hello again,

The path I used was just an example. To clarify, let's say we have the HTML Path /HTML/Table(1)/BODY(4). 

We know that the number following Table changes value (i.e. it could be 1,2,3,4... etc) for different elements in the page. However, we are not sure what number the element we search for will have, that is it could be:

/HTML/Table(1)/BODY(4) 
or 
/HTML/Table(2)/BODY(4)
or
/HTML/Table(3)/BODY(4)
etc.

We want to use regex to match an element that has this HTML Path structure (Note: another unique attribute should be specified, otherwise Blue Prism would match several elements)

To use the regex option on the Path-attribute of the spied element, you will have to write the HTML Path attribute in Application modeller as:

\/HTML\/Table\(\d{1,}\)\/BODY\(4\)
or
\/HTML\/Table\([0-9]+\)\/BODY\(4\)

\d{1,}
and [0-9]+ matches against 1 or more numbers (in a row) between 0 and 9

You have to use \ before "A character that otherwise would be interpreted as an unescaped language construct should be interpreted literally"

Here is the link to .NET documentation regarding regex https://docs.microsoft.com/en-us/dotnet/standard/base-types/character-escapes-in-regular-expressions

Kind regards,
Ivan

------------------------------
Ivan Khudur
------------------------------

Thnaks Ivan for your response. I will try and let you know if any further issue.

------------------------------
Amlan Sahoo
RPA Consultant
Equinix
------------------------------
Regards,
Amlan Sahoo

Hi Ivan,

I've trying to add the Regex expression to my attribute as follow

/HTML[1]/BODY[1]/DIV[\[0-9]+\]/DIV[5] however Blue Prism is prompting the error "Error - Highlighting results - Exception has been thrown by the target of an invocation."

I think that it's related to the character " \". Could you please provide some input regarding this?

------------------------------
Jose Bertozzi
------------------------------

Hello Jose,

You need to add a backslash before all escape characters that are not an regex pattern as well as slashes.

In your case I assume the "default" HTML attribute path looks something like this /HTML[1]/BODY[1]/DIV[1]/DIV[5].
To match exactly this HTML attribute path you need to but a backslash infront of the characters /[] :

/HTML[1]/BODY[1]/DIV[1]/DIV[5]    →    \/HTML\[1\]\/BODY\[1\]\/DIV\[1\]\/DIV\[5\]

From here you can replace the value of the first DIV element with your character class and its quantifier [0-9]+:

\/HTML\[1\]\/BODY\[1\]\/DIV\[[0-9]+\]\/DIV\[5\]

Note that you should not use backslashes for the character class's brackets or the quantifier.

I would suggest to narrow down the possible matches as much as possible, as the regex pattern [0-9]+, will match a number 1 or more times. This means it could match e.g. /HTML[1]/BODY[1]/DIV[100000]/DIV[5].
If you know that the value of the DIV element ranges between 0 and 99, you could replace [0-9]+ with [0-9]{1,2}.

Another thing you could try to narrow down the matches is to add anchors at the beginning and the end of your HTML Path like this:

^\/HTML\[1\]\/BODY\[1\]\/DIV\[[0-9]{1,2}\]\/DIV\[5\]$

This way you should avoid matching your HTML path in another HTML path, that has your HTML path as a substring.

Here are links to the .NET documentation regarding regex quantifiers and anchors:
https://docs.microsoft.com/en-us/dotnet/standard/base-types/quantifiers-in-regular-expressions
https://docs.microsoft.com/en-us/dotnet/standard/base-types/anchors-in-regular-expressions

Hope this helps!

Kind regards,
Ivan

------------------------------
Ivan Khudur
------------------------------