XPath, differentiating between two similar links

Ask general questions here.
Fergal
Certified Professional
Certified Professional
Posts: 298
Joined: Tue Feb 18, 2014 2:14 pm
Location: Co Louth, Ireland
Contact:

XPath, differentiating between two similar links

Post by Fergal » Wed Apr 01, 2015 2:17 pm

Hi,

[Ranorex 5.2.2 on Windows 7]

I have an XPath which ends with the following:
a[@innertext='Delete']
It works perfectly to find a link on a web page, with the anchor text "Delete".

When thereis more than one link, with the same "Delete" anchor text, I want the XPath to identify the first one on the web page. How can I do that?

Thanks!

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

Re: XPath, differentiating between two similar links

Post by krstcs » Wed Apr 01, 2015 3:29 pm

First, without seeing a snapshot of your application and the structure of your repository above this element it would be hard to know what you are working with. Please post those or send them to [email protected].

Second, Ranorex always finds the first match for a given RanoreXPath, so as long as you always want the first link, you should be able to leave it the way it is. You could also add '[1]' to the end of your current path.

Code: Select all

a[@innertext='Delete'][1]
But, again, that depends on how you have structured the repository and how the AUT is structured.
Shortcuts usually aren't...

Fergal
Certified Professional
Certified Professional
Posts: 298
Joined: Tue Feb 18, 2014 2:14 pm
Location: Co Louth, Ireland
Contact:

Re: XPath, differentiating between two similar links

Post by Fergal » Thu Apr 02, 2015 8:50 am

Thanks very much krstcs.
...as long as you always want the first link, you should be able to leave it the way it is...
My issue with that is that when the XPath identifies two elements for the same XPath, within say a click action, it doesn't know which one to click, so it doesn't click on either of them.
You could also add '[1]' to the end of your current path
That seems to be exactly what I'm looking for and I will try that next time I encounter the issue.

Thanks again!

CookieMonster
Certified Professional
Certified Professional
Posts: 74
Joined: Mon Aug 14, 2006 7:17 pm
Location: CH

Re: XPath, differentiating between two similar links

Post by CookieMonster » Thu Apr 02, 2015 9:04 am

Hi Fergal,

Please ask your web developer to add an unique id to the a tag, so you are language independent and you don't have to use indexes and makes the XPath robust.

But may you can use the href property to differentiate the a tags!?!?


Regards
Dan