How to search DOM elements using XPath or CSS selectors in Chrome Developer Tools?

74,816

Solution 1

You can use $x in the Chrome javascript console. No extensions needed.

ex: $x("//img")

Solution 2

Just typing xpath expression in the search box should work. It works for me in tip-of-tree build.

The feature seems to be broken in Chrome 11 though, I've filed a bug on this: http://crbug.com/79716

Solution 3

For xpath searches use $x('xpathSelector'). For a css selector use $('cssSelector').

However, this last selector returns only the first matching element. If you want to see all matching elements go for $$('cssSelector')

Share:
74,816
Bobo
Author by

Bobo

Updated on July 13, 2021

Comments

  • Bobo
    Bobo almost 3 years

    The doc http://code.google.com/chrome/devtools/docs/elements.html says it supports XPath or CSS selectors, but when I tried, didn't seem to work for me.

    Any one knows how to use it?

  • Bobo
    Bobo about 13 years
    you are right. it is working but the highlighting feature is broken. I'm using chrome 10.0.* on Mac os X.
  • Adolph Trudeau
    Adolph Trudeau about 12 years
    This is a helpful answer. To add to it, the $x function accepts a second optional argument, context. $x(xpath, context) This allows you to select a particular iframe content, for example, and run an xpath query against it. So for the first iframe: myframe = document.getElementsByTagName("iframe")[0].contentWindow.doc‌​ument.body; #to xpath query that iframe for table cells: $x("//td",myframe);
  • user3399101
    user3399101 almost 11 years
    better answer by Mark Polito below.
  • Dmitry Koroliov
    Dmitry Koroliov over 10 years
    to find an element with a CSS selector a one should use $$ console function, e.g. $$('body')
  • Dmitry Polushkin
    Dmitry Polushkin over 10 years
  • Bobo
    Bobo over 10 years
    coming back to this question after almost 2 years, yeah this one is nicer.
  • eeezyy
    eeezyy over 10 years
    It works with Chrome 32. Go to Element tab of the devtool and press CTRL+S and search for the xpath
  • Box
    Box over 7 years
    @eeezyy you mean ctrl + f ?
  • Otto G
    Otto G about 6 years
    Very good for debugging XPaths! By the way, the $x() function works in the Safari command line API, too.
  • Steven
    Steven about 6 years
    Excellent! I was looking for a way to do it in the elements pane, but this is much easier
  • Eaten by a Grue
    Eaten by a Grue about 5 years
    Works with Firefox Developer Tools as well
  • Matt Polito
    Matt Polito over 4 years
    @billynoah It's a common pattern now, should work in most browsers' dev tools.