Google Chrome copy CSS Path in Developer Tools

六月ゝ 毕业季﹏ 提交于 2019-11-28 08:58:42

Chrome has updated this option

In chrome after recent update this option has been changed from
(right click on the element in Elements Window) > Copy CSS path
to :
(right click on the element in Elements Window) > Copy > Copy selector

You can right click on the element in the main source window and "Copy CSS path". This has been a life saver when I have to work on pages that I can't re-code.

MattDiamant

Chrome doesn't have it, so people have made chrome extensions, bookmarklets, and other tools for Chrome to replicate this functionality.

Possible duplicate: Chrome equivalent of Firefox Firebug CSS select path

Bookmarklet: http://www.selectorgadget.com/

Chrome Extension: https://chrome.google.com/webstore/detail/lbghbpofdlcecfbpjgmffnkieenjkboi

I would still like other people's answers, suggestions, and tips on how to best deal with this in Chrome.

Here is a small (CoffeeScript) snippet that will give CSS path (up to first id element - though you can easily change that by removing the break part):

getCSSPath = (node)->
  parts = []
  while node.parentElement
    str = node.tagName
    if node.id
      str += "##{node.id}"
      parts.unshift str
      break
    siblingsArr = Array.prototype.slice.call(node.parentElement.childNodes)
    ind = siblingsArr.filter((n)-> n.attributes?).indexOf(node)
    parts.unshift str + ":nth-child(#{ind + 1})"
    node = node.parentElement
  parts.join ' > '

Uses ":nth-child" for every node, so you need to modify it if you'd like to get also class names.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!