When to use xpath and why?

85 views
Skip to first unread message

Magus

unread,
Mar 21, 2012, 6:10:38 AM3/21/12
to webd...@googlegroups.com
From my own working experience and tons of articles on internet, "xpath" seem like a bad stuff that everyone should avoid using it.
But I think "xpath" must be a reasonable stuff in web test automation, or it must be already removed from the API.

I want to know that under what kind of situation, using xpath is better than ID, css selector, etc...
Can someone show me an example?

Thank you very much.

Jakub Siberski

unread,
Mar 21, 2012, 8:22:47 AM3/21/12
to webd...@googlegroups.com
When you have static parent but dynamic content? Then you have your constant root, and you can find dynamically created children by xpath. 

Think about something like newsfeed. You have constant div where items will be inserted, but items themselves may have different order and different content. 
Additionally you may not have control over some of items generated. There can lots of different difficulties there.

I wouldn't say that xpath is better that other selectors. I would say it is last resort when dealing with dynamic content.


--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/O5inSY0k5YwJ.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.



--
Pozdrawiam
Jakub Siberski

Ross Patterson

unread,
Mar 21, 2012, 8:39:42 AM3/21/12
to webd...@googlegroups.com

XPath is a tool.  Every tool has purposes it’s useful for, and purposes for which it is commonly misused.  Lock picks allow locksmiths to let you in when you left your keys in the coat you lent to the pretty girl in a strapless dress on a cold winter evening (trust me), but they also allow people to break in to your house and steal your TV.  Part of being a professional is being able to judge which of the many tools in your toolkit are appropriate for the task at hand.  I own a variety of hammers, because a baby sledge makes a poor (but satisfying!) tack hammer, and a drywall hammer is does a nice job of dimpling wallboard so the nails can be hidden.

 

XPath selectors can climb back up the tree, with its “/../” construct, something most other selectors (including CSS) can’t.  They can include Boolean logic within predicates (e.g., “[@x=’y’ or @a=’b’]”), which is also unusual (although CSS can do it for limited combinations).  They can reference the content of an element (e.g., via the “text()” and “.” constructs), which real CSS can’t (although some JavaScript CSS libraries can help).  The list goes on and on.

 

Ross

Tarun Kumar

unread,
Mar 22, 2012, 1:29:26 AM3/22/12
to webd...@googlegroups.com
Nice analogy

Magus

unread,
Mar 22, 2012, 4:05:35 AM3/22/12
to webd...@googlegroups.com
Thank you Ross.
Reply all
Reply to author
Forward
0 new messages