29-04-24 10:33 AM
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
Answered! Go to Answer.
06-05-24 09:53 PM - edited 06-05-24 10:02 PM
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.
29-04-24 02:01 PM
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']
06-05-24 06:47 PM
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 !
06-05-24 09:53 PM - edited 06-05-24 10:02 PM
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.
08-05-24 04:20 PM
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.
15-05-24 07:51 PM
That's a more concise function
Thanks @MichealCharron . Keep adding to it so we keep learning !