XPath vs CSS Performance

750 views
Skip to first unread message

semperos

unread,
Jan 25, 2012, 12:40:55 PM1/25/12
to selenium-...@googlegroups.com
What are the current performance differences between using XPath or CSS queries with the various WebDriver drivers for finding elements on the page?

Metrics preferred, from-the-hip analysis from folks who work on the code also welcome.

-Daniel

Daniel Wagner-Hall

unread,
Jan 26, 2012, 8:55:04 AM1/26/12
to selenium-...@googlegroups.com
Short story: Prefer CSS to XPath.

Metrics would be interesting, although would inherently be
micro-benchmarks, and probably quite page-specific. Please do provide
some, I'd be interested to see them!

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

semperos

unread,
Feb 2, 2012, 2:47:02 AM2/2/12
to selenium-...@googlegroups.com
For cross-browser support, is WebDriver's CSS querying more robust/consistent than XPath? And for that matter, do we have a CSS grammar, even informal, that we can rely on for WebDriver?

This is less a "complaint" about WebDriver documentation, as my own ignorance about competing CSS-style query languages.

Adam Goucher

unread,
Feb 2, 2012, 7:39:53 AM2/2/12
to selenium-...@googlegroups.com
On 12-02-02 2:47 AM, semperos wrote:
> For cross-browser support, is WebDriver's CSS querying more
> robust/consistent than XPath? And for that matter, do we have a CSS
> grammar, even informal, that we can rely on for WebDriver?
http://www.w3.org/TR/css3-selectors/

In RC the grammar was Sizzle (http://sizzlejs.com) which is both a super
and subset of the W3C spec. There are a couple pathological situations
where WebDriver will fall back to Sizzle, but the general rule-of-thumb
is write W3C selectors.

-adam

Daniel Wagner-Hall

unread,
Feb 2, 2012, 6:29:45 PM2/2/12
to selenium-...@googlegroups.com
We expose what the browsers expose, through document.querySelector.
If the browsers don't implement querySelector at all, we patch in CSS3
support, but basically, it's down to what your browser supports.

> --
> You received this message because you are subscribed to the Google Groups
> "Selenium Developers" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/selenium-developers/-/qv1GBT8wTLoJ.

semperos

unread,
Feb 4, 2012, 2:37:34 AM2/4/12
to selenium-...@googlegroups.com
One feature present in XPath that I see missing from the CSS spec is the ability to check against the "text" of a given element.

I see an older post here about using the :contains() pseudo function, but trying that in the latest Firefox doesn't work for me. Is there a reliable alternative to XPath's text() function?

Leo Laskin

unread,
Feb 4, 2012, 8:43:42 AM2/4/12
to selenium-...@googlegroups.com

In webdriver there is element.text...  however there is not a specific way to reference text when searching by css.

On Feb 4, 2012 2:37 AM, "semperos" <daniel.l...@gmail.com> wrote:
One feature present in XPath that I see missing from the CSS spec is the ability to check against the "text" of a given element.

I see an older post here about using the :contains() pseudo function, but trying that in the latest Firefox doesn't work for me. Is there a reliable alternative to XPath's text() function?

--
You received this message because you are subscribed to the Google Groups "Selenium Developers" group.

Adam Goucher

unread,
Feb 4, 2012, 4:16:38 PM2/4/12
to selenium-...@googlegroups.com
:contains() is a sizzle-ism, not standard css which is why its not working.

-adam

> --
> You received this message because you are subscribed to the Google
> Groups "Selenium Developers" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/selenium-developers/-/g-ATPxV0RKQJ.

semperos

unread,
Feb 5, 2012, 8:21:00 PM2/5/12
to selenium-...@googlegroups.com
Yeah, that's what I gathered, but was curious if there did exist a "legitimate" alternative.

Santiago Suarez Ordoñez

unread,
Mar 3, 2012, 4:05:15 PM3/3/12
to selenium-...@googlegroups.com
To add some more to this topic, I did some investigation around this last year. Mostly focused in Selenium 1, which relies in external libraries for the queries. I don't think this is a much valuable topic, we WebDriver basically just relies on each browser's implementation (except for IE), so I'm guessing numbers will vary a lot from one to another.

In case you're still interested, here are my conclusions from back then:
http://saucelabs.com/blog/index.php/2011/05/why-css-locators-are-the-way-to-go-vs-xpath/

This is the raw dump of timings:
https://gist.github.com/963821

I think I still have the python script I used to capture all these. I remember it being a bit messy, which prevented me from putting it up in github. I'm definitely willing to share it if you're interested, though.

Best,
Santi


On Sun, Feb 5, 2012 at 5:21 PM, semperos <daniel.l...@gmail.com> wrote:
Yeah, that's what I gathered, but was curious if there did exist a "legitimate" alternative.

--
You received this message because you are subscribed to the Google Groups "Selenium Developers" group.
Reply all
Reply to author
Forward
0 new messages