Pro and Cons of XPath.

730 views
Skip to first unread message

Sairam Prasath

unread,
Jul 18, 2012, 3:10:24 AM7/18/12
to seleniu...@googlegroups.com
Hi All,
Can you please list me the pros and cons of XPath?

Regards,
Sairam Prasath.

Ravi Gupta

unread,
Jul 18, 2012, 3:31:03 AM7/18/12
to seleniu...@googlegroups.com
Pros and Cons which I know for XPath :

Pros :
Able to locate elements which are difficult to locate  even with the standard conventions

Cons :

Very slow in locating the elements on the page.
Multiple values need to be put in an arrays .. SO array handling is important.

Peter Gale

unread,
Jul 18, 2012, 3:36:48 AM7/18/12
to Selenium Users
> Able to locate elements which are difficult to locate  even with the standard conventions
Well, yes, the pro's and cons depend on what you're comparing it to.



> Very slow in locating the elements on the page.
XPath is known to be slower to run than CSS in IE, but I've not heard anyone claim that it is slower in all browsers.


> Multiple values need to be put in an arrays .. SO array handling is important.
The same cioudl be said about any locator strategty (i.e. XPath, CSS, ByID etc) so it's not really a con of Xpath as such.


Date: Wed, 18 Jul 2012 00:31:03 -0700
From: canceri...@gmail.com
To: seleniu...@googlegroups.com
Subject: [selenium-users] Re: Pro and Cons of XPath.
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/q7oBe9fgrFkJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Darrell Grainger

unread,
Jul 18, 2012, 9:44:05 AM7/18/12
to seleniu...@googlegroups.com
Pro:

- Allows you to back up through the DOM (e.g. "..")
- Allows you to match against CDATA in a tag (e.g. text())

Con:

- Typically slower than other locators on Internet Explorer

General rule for me is use By.id whenever possible. If I need css selectors or xpath to narrow match to one element, use css selector unless I must use xpath. Must use xpath if you need to find an element, back up through the DOM then down to the element you REALLY want or if I need to match against the visible text (e.g. text()).

For example,

    <a src="http://www.google.ca">
        <img src="google_button.gif" title="Google Button"/>
    </a>

If I can guarantee the button has a tooltip "Google Button", which is visible to the user then a good locator would be: "//img[@title='Google Button']/.." This finds the image then goes up one level to the anchor. Another classic example of using xpath:

    <div tabindex="0" role="button" class="GFUCT3GPN GFUCT3GFP GFUCT3GI5" id="b_post0" aria-pressed="false">
        <input type="text" indextab="-1" style="opacity: 0; height: 1px; width: 1px; z-index: -1; overflow: hidden; position: absolute; ">
        <span class="GFUCT3GDO">Post</span>
    </div>

I know the 'button' will always have the text "Post" so I'd use the xpath, "//span[text()='Post']/../input". There is no way I can match against the text using css selectors.
Reply all
Reply to author
Forward
0 new messages