Good tools for generating relative XPaths?

207 views
Skip to first unread message

QA_manager

unread,
Jun 11, 2010, 6:13:41 PM6/11/10
to Selenium Users
I see plenty of questions about XPath, how to write good XPath
expressions, and how to find one element in relation to an easy-to-
find element. For example, with this HTML for a single row in a
table:

<td>
<a href='editBucket.do?subnet=64.0.0.0'>Selenium_Test1</a>&nbsp;
</td>
.
.
.
<td>
<img class="state" src="/images/icn_status_grn.gif">
IN
</td>
<td>
<img class="state" src="/images/icn_status_grn.gif">
IN
</td>

The first node (Selenium_Test1) is easy to find, but the other nodes
are the same (duplicated) in every row of the table, so they can only
be (reliably) found in relation to Selenium_Test1. That is, the table
might look like this:

Selenium_Test1, img, text, img, text
Selenium_Test2, img, text, img, text
Selenium_Test3, img, text, img, text

In order to find the text of the first <td> node that is in the same
row as Selenium_Test2, relative XPath is the only solution I know.

So, are there any tools that allow you to click on two elements and
get a list of possible XPath relational expressions? In this case a
list of relative paths from Selenium_Test1 to the text of the <td>
node might include:

/../following::td[1]
/../following-sibling::td[1]
/../following-sibling::*[img[@class='state']][1]

All three of these work. IMHO, the first is the most fragile; it will
break if another <td> column is inserted between our two targets, or
into their children. The second is not much better, but the third is
fairly robust - it will find the first image with 'class=state' that
follows our parent node, and then find the text from that. Moving the
columns around won't affect it (unless it is moved _after_ the other
image node).
.
I know that XPather says it generates relative XPaths, but I can't get
it to generate anything approaching the examples above. So:
- Is XPather capable of generating a list of relative paths similar
to the one I have outlined here?
- If not, are there other tools that will?

I know that this is a _hard_ problem - trying lots of possible
combinations, figuring out which ones work, and then rank ordering
them in some way - but I know I am not the only one who would like to
have it! :-)

TIA.

code_warrior

unread,
Dec 30, 2013, 5:45:21 AM12/30/13
to seleniu...@googlegroups.com
Any updates on this one? I am working on something similar.

Faraz Mohammad Khan

unread,
Dec 31, 2013, 12:13:23 AM12/31/13
to seleniu...@googlegroups.com
I always use FirePath for generating xpath expressions and it works perfectly. What you can do in Firepath is check the option to generate absolute xpath. This will rid you from hard coded IDs(they can change anytime) and give you a more generic xpath. Try it out.

nani.thrinath.info

unread,
Dec 31, 2013, 8:40:40 AM12/31/13
to seleniu...@googlegroups.com
Try Absolute xpath with firebug.



Thrinath


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/65e22e26-0a2c-49a7-80fb-a07a48cfb198%40googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.



--
Thrinath

Krishnan Mahadevan

unread,
Dec 31, 2013, 10:02:40 AM12/31/13
to seleniu...@googlegroups.com
Absolute paths are brittle. Suggesting usage of them is not a good idea. 
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CACy7ZuG-EY3SZzOawc3hmsETKbtKmQf86nMYUAP9PbX71Eh4PA%40mail.gmail.com.

For more options, visit https://groups.google.com/groups/opt_out.


--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/

David

unread,
Jan 2, 2014, 1:15:23 AM1/2/14
to seleniu...@googlegroups.com
I don't think there exists such a nice tool yet. Nor whether building one is feasible or if anyone is currently working on creating such a tool.

For now, in such relative XPath cases, the best option is always to use desired XPath tool (Firebug, XPather, FirePath, etc.) and select/inspect the desired element. Then generally ignore the given XPath that's generated by the tools, then inspect the HTML DOM source around the inspected element from Firebug and like tools and manually figure out the correct XPath to create that uses relative referencing (location strategy). And for such purposes, FirePath is very good because it has an XPath tester where it shows you the auto-generated XPath for the element that was inspected. You can modify it and test whether the changes work as intended or not. That's not available in standard Firebug alone, nor XPather, I believe.
Reply all
Reply to author
Forward
0 new messages