Search code examples
javascripthtmlcss-selectorsgoogle-chrome-devtoolsfirefox-developer-tools

Web Developer mode in Chrome or Firefox: how to get an object's CSS full selector?


When I have the Web Developer tool open and inspecting a page, sometimes I wish to select an element and copy (i.e. grab the entire text) of its CSS selector.

I mean this:

screenshot

For some reason the text in that box is not selectable, and right click shows no menu or options.

Is there a way to grab or export that text so I can process it in my code?


Solution

  • Right-click the element in the inspector, choose the Copy submenu, and each browser gives you slightly different options:

    • Chrome (and other Chromium-based browsers including the new Microsoft Edge) gives you the option to copy the selector, which produces a somewhat optimized but still fairly long selector that will uniquely identify the element.

    • Firefox gives you two options: "CSS Selector" that functions mostly the same as Chrome but gives you a much more optimized selector, and "CSS Path" that gives you the same as what you're pointing to in your screenshot, the entire path to that element starting from the root. "CSS Path" is designed for web testing tools but it's probably excessive for most other use cases, which I'm guessing is why Chrome doesn't have a similar feature.

    Check out these other answers in which I explain a bit more how these features work: