Find XPath or RXPath of an element

Technology specific object identification, supported applications, web technologies, and 3rd party controls.
ArsenalFC
Posts: 1
Joined: Fri Oct 30, 2015 6:29 pm

Find XPath or RXPath of an element

Post by ArsenalFC » Fri Oct 30, 2015 6:35 pm

Hi Team,

How do I find RXPath or XPath of an UI element? I found an element based on its Type and class. I would like to pass this element to some Ranorex function or method which would give me the XPath or RXPath expression? How do I achieve this in Ranorex? I need some function which would "Get RXPath of an UI Element". Please help.

Thanks.

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

Re: Find XPath or RXPath of an element

Post by odklizec » Tue Nov 03, 2015 8:50 am

Hi,

In my opinion, what you are looking for is Find method (or its equivalent, like FindSingle, etc...) ?
http://www.ranorex.com/Documentation/Ra ... t_Find.htm
http://www.ranorex.com/Documentation/Ra ... Single.htm

This method should help you to find element or elements by given properties. Once found, you can pass the found element to another method using "Adapter" type of argument. There is no need to pass rxpath, which is just a string. But if you insist on obtaining the rxpath from found element, you can use GetPath method to do so.

Quick sample...

Code: Select all

var element = Host.Local.FindSingle(.//*[@class='something' and @type='something']); //this returns first found element
string xPath = element.GetPath(); // this should return xpath of found element 
You can find some more examples of using Find or FindSingle method in Ranorex code samples or by searching this forum.
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

Hondo
Posts: 43
Joined: Thu Nov 03, 2016 12:03 am

Re: Find XPath or RXPath of an element

Post by Hondo » Wed Jun 27, 2018 5:42 pm

odklizec wrote: Quick sample...

Code: Select all

var element = Host.Local.FindSingle(.//*[@class='something' and @type='something']); //this returns first found element
string xPath = element.GetPath(); // this should return xpath of found element 
I realize this is ~3 years old.. so have a quick question.

I'm trying this out as a proof of concept for a Log Playback script. => Load up a log file as a CSV datasource > parse out the log lines with automation ID's > Locate the element in the Repository with the Automation ID > Get THAT XPath > fire an event on the element.

UI events are being logged with Automation ID's.
e.g. Log Message: 06/25/2018 09:24:05 TRACE Blah.bblah.bablah.blah_Click: blahClickID

The same Automation ID's are being referenced in the Repository.
e.g. .//button[@automationid='blahClickID']

I'm successfully parsing out the Automation ID's from each log line and then sending it to my SearchRepository Method.

This line appears to work: var element = Host.Local.FindSingle(".//*"+searchStr); //this returns first found element

This line throws an error / won't build: string xPath = element.GetPath(); // this should return xpath of found element
No overload for method 'GetPath' takes 0 arguments (CS1501) -

If element.GetPath() can return the XPath.. that would be perfect.

Thanks!

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

Re: Find XPath or RXPath of an element

Post by odklizec » Thu Jun 28, 2018 8:20 am

Hi,

The problem is, that Element.GetPath takes some parameters, which must be filled. Try this...

Code: Select all

string xPath = element.GetPath(PathBuildMode.Default);
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

Hondo
Posts: 43
Joined: Thu Nov 03, 2016 12:03 am

Re: Find XPath or RXPath of an element

Post by Hondo » Thu Jun 28, 2018 7:40 pm

odklizec wrote:Hi,

The problem is, that Element.GetPath takes some parameters, which must be filled. Try this...

Code: Select all

string xPath = element.GetPath(PathBuildMode.Default);
Cool thanks. Got there last night after reviewing the library documentation online. Tried the .Simple member too.
I also had to use the .ToString() method in order to report to the log. Seemed a little redundant.

Code: Select all

string xPath = element.GetPath(PathBuildMode.Default).ToString();
Script builds and runs, but not finding the element in the Repository.
My Repository is structured with Rooted folders. So just need traverse the tree.
Can you recommend the best/quickest way to do this?

I saw another years old post from support that recommended flattening out the Repository to make it easier.
Suppose I could do that too.

Thanks again!

Hondo
Posts: 43
Joined: Thu Nov 03, 2016 12:03 am

Re: Find XPath or RXPath of an element

Post by Hondo » Sat Jun 30, 2018 1:30 am

Poking at this at the end of my day again.

What I've realized is that using "Host.Local" is using the Desktop and finding the xpaths in the application.
That works. Slowly, but it works. Employing the Unknown element adapter can perform actions on elements.

Need to redirect to the search to the Repository. So far, finding that difficult.

Hondo
Posts: 43
Joined: Thu Nov 03, 2016 12:03 am

Re: Find XPath or RXPath of an element

Post by Hondo » Fri Jul 06, 2018 5:32 pm

Hondo wrote:Need to redirect to the search to the Repository. So far, finding that difficult.
So which is the best/most efficient method to use for searching the Repository through code? (with just an automationid)

Thanks!

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

Re: Find XPath or RXPath of an element

Post by odklizec » Fri Jul 06, 2018 5:50 pm

Hi,

I don’t think there is an easy way to search a repository element via code and then use the found repository element in code. There was a similar discussion some (long) time ago.

But this was discussed at a time, when there was not possible to pass the repo elements as method parameters. Honestly, I’m still not quite sure what exactly you want to achieve and why you don’t use repo elements via parameters or call them directly in code (the later one is not a very good idea). I think you somehow trying to reinvent the wheel? But I may be missing something important ;)
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

Hondo
Posts: 43
Joined: Thu Nov 03, 2016 12:03 am

Re: Find XPath or RXPath of an element

Post by Hondo » Fri Jul 06, 2018 9:35 pm

odklizec wrote:Honestly, I’m still not quite sure what exactly you want to achieve and why you don’t use repo elements via parameters or call them directly in code (the later one is not a very good idea). I think you somehow trying to reinvent the wheel? But I may be missing something important ;)
Nope, you're fine. It's not normal. Working backwards in a way.

I'm trying to create a playback script from the Application Log.
This would be VERY valuable for tracing exactly what a user did when they encountered a problem/error.

The application log lines are complete with unique automationID's (for most UI controls).
The automationID's are defined properties in the XPaths of the Repository elements.

I'm parsing the application log and striping out the automationID's.
Next I want to search the Repository for the element with the automationID and then execute a Button Press or Click action on it.

Like I said, I got the script working using Host.Local (going straight to the AUT), but it was very S.L.O.W..

If searching the Repository is a non starter, is there a way I can speed up using Host.Local?
My other option is to use a pivot/lookup file to resolve the automationID to the full XPath and execute.

Thanks!

p.s. I was considering renaming the repo elements to equal the automationID and calling them direclty, but it sounds like you're recommending against doing that.

Maybe tell me more about using "repo elements via parameters".

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

Re: Find XPath or RXPath of an element

Post by odklizec » Sat Jul 07, 2018 8:53 am

Hi, I now understand. I think the only way to achieve what you want is using the method described in the link I posted in my previous post. Using the xpaths or repo elements directly in code means, that you will have to update them manually every time this or that repo element changes. I mean, if you remove, rename or move a repository element to another folder/root folder, your code will not update automatically. Only if you change the repo element’s xpath, such change will reflect in your code as well. And if you use xpaths in code, these will not auto-update at all, which means, your code may soon be a mess of useless xpaths. This is why it’s better to use repository and always pass the repo elements as method parameters.
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