How to get Xpath (not RXpath) of repository item?

Technology specific object identification, supported applications, web technologies, and 3rd party controls.
Posts: 245
Joined: Mon Mar 16, 2020 11:31 am

How to get Xpath (not RXpath) of repository item?

Post by mrt » Fri Sep 24, 2021 2:43 pm

Dear all,

I wonder how it is possible to get the FULL XPATH from a repository element (not the RxPath).

RxPath identified in Spy or by Ranorex recording:

Code: Select all

/dom[@domain='']//div[#'rx-header-sticky-content-wrapper']//img[@alt='Ranorex Logo']
Full Xpath identified by Chrome browser DevTools:

Code: Select all

Reason is I need to retrieve attributes of an element which has no ID via javascript.
The only (best) possibility I found was to use document.evaluate() but it complains when inputting RxPath that it is no valid Xpath, this seems to be related to the hash '#' symbol. When replaced by 'id=' the path seems to be valid, but the element is not found with the RxPath.

The Full Xpath copied from Chrome devtools works indeed.


User avatar
Ranorex Guru
Ranorex Guru
Posts: 7469
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: How to get Xpath (not RXpath) of repository item?

Post by odklizec » Mon Sep 27, 2021 7:18 am


As far as I know, there is no way to generate pure xpth with Ranorex. The xpath provided by chrome dev, tools may eventually work, but it will definitely fail at some point in time, because such xpath is very fragile.

Anyway, you may force Ranorex not to use unique IDs, by disabling this option in Settings (General tab). Another possibility how to change the way Ranorex creates rxpath, is to change the RanoreXPath settings (Advanced tab). If you move the slider to left, it will create similar (if not the same) xpath, as you get with chrome dev tools. But as mentioned, such path is fragile and will, most probably, fail in future.

From my experience, there are almost always some attributes. The thing is, one need to edit the xpath weight, to tell Ranorex to pick them up, instead of some other attributes ;) A typical example is a "class" attribute, which often contains many useful details (typically in Angular-based apps).
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

Posts: 245
Joined: Mon Mar 16, 2020 11:31 am

Re: How to get Xpath (not RXpath) of repository item?

Post by mrt » Mon Sep 27, 2021 7:32 am

Yes, usually there are unique attributes somewhere for identification.
In this case I have about 10 elements of the same type, they all share the same class attributes and values.

But, I have found another solution where I do not specify the full xPath, so document.evaluate can't complain about it. ;)
I just specify the //tag, javascript is quite fast on identifying the right element by name and then I return the attribute i am interested in:

Code: Select all

IFrameTag myIframe = repo.MyDom.MyIframeItem.Self; 
string escaped = HttpUtility.JavaScriptStringEncode("//my-custom-tag[contains(., '" + myDisplayedName + "')]");
scriptCommand = "return document.evaluate('" + escaped + "', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.myCustomAttribute";
var theCustomAttribute = myIframe.ExecuteScript(scriptCommand);