55

I want to be able to select/highlight an element on the page and find its selector like this:

div.firstRow div.priceAvail>div>div.PriceCompare>div.BodyS

I know you can see the selection on the bottom after doing an inspect element, but how can I copy this path to the clipboard? In Firebug I think you can do this, but don't see a way to do this using the Chrome Developer Tools and search for an extension did not turn-up anything.

This is what I am trying to do for more reference: http://asciicasts.com/episodes/173-screen-scraping-with-scrapi

0

5 Answers 5

59

Chrome Dev Tools CSS Path

If Chrome Dev tools if you select the element in the source pane and right click, then you will see the "Copy CSS Path" option.

In newer versions of Chrome, this is (right-click) > Copy > Copy selector.
You can also get the XPath with (right-click) > Copy > Copy XPath

5
  • 8
    I don't have this option in my chrome Apr 13, 2016 at 0:15
  • 4
    It's now called Copy Selector
    – TomC
    Mar 28, 2017 at 16:27
  • 11
    Right click > Copy > Copy Selector in Chrome 61
    – OscarRyz
    Oct 16, 2017 at 21:58
  • This answer is outdated. Look @OscarRyz comment.
    – Philippe
    Dec 31, 2020 at 3:22
  • 1
    Sadly Chrome has removed this feature (no longer exists - they broke the Copy Selector so it now does something completely different, it picks the minimal possible selector, making it useless). Doesn't appear to be a replacement
    – Adam
    Jan 10 at 9:25
26

Although not an extension, I did find a bookmarklet called Selector Gadget that does exactly what I was looking for.

1
13

The workflow I currently follow to get CSS selectors from elements with the latest Chrome version (59) is as follows:

  1. Open Chrome Dev tools (cmd/ctrl + alt + j):

Step 1 - Opening Chrome Dev tools

  1. Click on the select element tool in page (cmd/ctrl + alt + c):

Step 2 - Clicking on the select element tool in page

  1. Click on the element you want to get the selector from in order to view it in the dev tools panel:

Step 3 - Selecting the element

  1. Right click on the dev tools element:

Step 4 - Right clicking on the element

  1. Click on Copy -> Copy selector:

Step 5 - Copy selector

Which gives me the following:

#question > table > tbody > tr:nth-child(1) > td.postcell > div > div.post-text > blockquote > p

1
  • 3
    Too many screenshots. Only the last one is necessary to answer OP question.
    – Philippe
    Dec 31, 2020 at 3:23
5

Do "Inspect Element" or Ctrl+Shift+I, it's at the VERY bottom of the screen. You can also type in the "Search Elements" box at the top-right of the dev tools if not sure about the selector.

2
  • Sorry, I wasn't clear enough. I know that is on the bottom, but I want a way to copy/paste it (using the selector format) as I need to do this for a number of items.
    – kale
    Dec 21, 2010 at 15:13
  • Unfortunately I'm not sure of a way to do this. Not really sure why you'd want to do that either since you probably want to apply some "cleanup" to the selector that Firebug/Chrome will give you. E.g. you wouldn't want to start every selector in your css with "html.body.div" when instead you can just say "div". Sorry can't help. Dec 21, 2010 at 15:50
2

It's sometime necessary to do this if you have a very complex app structure that you inherited and are trying to track down a very tricky multi nested css depth problem. Jquery mobile pre 1.3 would be a good example of this. Bootstrap apps etc..

I tried the above tool but could not get that to actually select the entire parent and children of a complex inheritance similar to the original posters question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.