How to fix object properties in xPath

Technology specific object identification, supported applications, web technologies, and 3rd party controls.
kaancha
Posts: 17
Joined: Wed Oct 22, 2014 4:12 pm

How to fix object properties in xPath

Post by kaancha » Wed Oct 22, 2014 4:21 pm

Hi, I am new to Ranorex and xPath.
I am having problems identifying objects which I recorded earlier. There has been a slight change in path to get to that page though.
I think there should be a way to fix objects in xPath without having to re-record them all.

Here is my Original xPath:
/dom[@domain='abc.com']//li[#'S32M68_3_1']/?/?/a[@innertext='Advanced Search']

Here is the New xPath:
/dom[@domain='abc.com']//li[#'S32M384_3_1']/?/?/a[@innertext='Advanced Search']

I noticed the only difference is in 2 digits number and 3 digits number (68 got changed to 384).

How can I fix this in xPath? Is it possible to fix all the objects at once (Globally) ?
If I assign variable how can I just dynamically change middle characters of the string?

Thanks for your help.

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: How to fix object properties in xPath

Post by krstcs » Wed Oct 22, 2014 5:14 pm

I would suggest that you look into using rooted folders. Instead of having each element's XPath contain the whole parent tree, you would put each item inside a rooted folder that is the parent XPath element.

For example use this:

Code: Select all

--> MyWeb : /dom[@domain='abc.com']
----> MyLiTag : //li[#'S32M68_3_1']        <--- This is a rooted folder.  All children will use it's path
------> MyATag : ?/?/a[@innertext='Advanced Search']
------> MyATag2 : ?/?/a[@innertext='Other Stuff']
This means that you could change the MyLiTag folder's path and it would update ALL children's paths to work. This will make management much easier in the long-run.


Also, I'm guessing that your LiTag is using a non-unique (and randomly variable) ID. I would suggest just removing that ID from the XPath, so it would be: "//li".


Edit to add:

You can also use regular expressions to filter out the part of the ID that will change. So, if you know that there might be some numbers in the middle that change, but there will always be the same things surrounding the numbers, you could do something like this:

Code: Select all

//li[@id~'S32M[0-9]*_3_1']
This says "Find an id attribute that has the form "S32M", followed by any number of any combination of numerals 0-9, followed by "_3_1".

Notice that you have to change the path to use the "@id~" nomenclature instead of "#". # is for unique ids only and can't be used with regex.
Shortcuts usually aren't...