Skip to content

Instantly share code, notes, and snippets.

@shobhit
Created October 19, 2022 05:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shobhit/e5c8d29c52c76aba93bfc1432801a5f2 to your computer and use it in GitHub Desktop.
Save shobhit/e5c8d29c52c76aba93bfc1432801a5f2 to your computer and use it in GitHub Desktop.
const getCssSelector = (el) => {
let path = [], parent;
while (parent = el.parentNode) {
path.unshift(`${el.tagName}:nth-child(${[].indexOf.call(parent.children, el)+1})`);
el = parent;
}
return `${path.join(' > ')}`.toLowerCase();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment