cancel
Showing results for 
Search instead for 
Did you mean: 

Xpath Usage in Blue Prism

swapnadeep_deb
Level 4

Hi all,

We have a requirement to use regular expression in Xpath for identification of an element. Now Xpath 1.0 doesn't support regex usage but it does from 2.0 version.

Could you please confirm if BP supports a higher version of Xpath presently.

 

BP version I am using = 7.1.1

Swapnadeep Deb
RPA Developer
TCS
1 BEST ANSWER

Best Answers

@swapnadeep_deb 

You could always use the XPath AND equivalent and the workaround for ends-with (another thing that does not exist in XPath v1.0). Something, for your example, like the following would work:

 

//div[starts-with(@id, 'abc-')]['-xyz'=substring(@id, string-length(@id) - string-length('-xyz')+1)]

 

Putting two or more filters together works like an AND.

The really ugly stuff is the workaround for the ends-with function which simply compares the text you want at the end of the id with a substring of the end of an id.

MichealCharron_0-1715028632063.png

 

Micheal Charron
RBC
Toronto, Ontario
Canada

View answer in original post

5 REPLIES 5

MichealCharron
Level 8

@swapnadeep_deb 

The support for XPath is in the browser itself and currently (and apparently forever) only XPath 1.0 is supported.

I don't know what kind of Regex you are looking for but if you are just simply wanting to look for a list of values, one very poorly documented feature of XPath 1.0 is the OR operator. You can, for example, look for an element that can flips between "Successful" and "Failure" by using the "|" OR operator with the following XPath:

//span[text()='Successful']|//span[text()='Failure']

 

Micheal Charron
RBC
Toronto, Ontario
Canada

swapnadeep_deb
Level 4

Hi @MichealCharron ,

That's a cool insight but my situation is kinda different. So supposedly there's an element with the html id = abc-1-xyz. Now here the 1 changes to a random number say 4 or 6 for the next appearances of the same element. Now here using an xpath as //*[@id='abc-\d+-xyz'] and doing a regex* match seems a stable approach than using something like //*[contains(@id,'abc'] as it will fail if any element appears in the root app with an id containing 'abc'

Now when I searched a bit I found the Xpath 1.0 doesn't support reg expressions. So the query on if v2.0 is supportable. If you say it's not then it's kinda sad and gotta find other ways to match the element !

Swapnadeep Deb
RPA Developer
TCS

@swapnadeep_deb 

You could always use the XPath AND equivalent and the workaround for ends-with (another thing that does not exist in XPath v1.0). Something, for your example, like the following would work:

 

//div[starts-with(@id, 'abc-')]['-xyz'=substring(@id, string-length(@id) - string-length('-xyz')+1)]

 

Putting two or more filters together works like an AND.

The really ugly stuff is the workaround for the ends-with function which simply compares the text you want at the end of the id with a substring of the end of an id.

MichealCharron_0-1715028632063.png

 

Micheal Charron
RBC
Toronto, Ontario
Canada

@swapnadeep_deb 

Sorry to dwell on this but this morning another solution popped into my head (I have to get a life). You could also use the following for your example:

//div[translate(@id,'0123456789','')='abc--xyz']

It is simpler because it just removes all the numbers out of the string for comparison.

MichealCharron_0-1715181504833.png

Micheal Charron
RBC
Toronto, Ontario
Canada

swapnadeep_deb
Level 4

That's a more concise function

Thanks @MichealCharron . Keep adding to it so we keep learning !

Swapnadeep Deb
RPA Developer
TCS