This package is a little piece of Google's Chromium DevTools project which modified to make it work on JSDOM.
The DOMPath is responsible for generating a selector given an element in a particular DOM hierarchy.
Its methods can be accessed via the following menu:
Open DevTools -> Navigate to Elements tab -> Right click on selected Element -> Copy
Installation
npm i -S chrome-dompath
DOMPath
Get selector, JS Path and XPath of a DOM element.
const DOMPath = require('chrome-dompath');
const dom = new JSDOM('<root><a><b>T1</b><c>T2</c></a></root>').window.document;
const element = dom.querySelector('b');
const element = window.document.querySelector('b');
DOMPath.fullQualifiedSelector(element, true);
DOMPath.jsPath(element, true);
DOMPath.xPath(element, true);
API
DOMPath.fullQualifiedSelector(node: DOMNode | optimized: boolean)
Returns the full qualified selector (string) for the node supplied.
DOMPath.fullQualifiedSelector(element);
DOMPath.fullQualifiedSelector(element, true);
DOMPath.jsPath(node: DOMNode | optimized: boolean)
Returns the JS path (string) for the node supplied.
DOMPath.jsPath(element);
DOMPath.jsPath(element, true);
DOMPath.xPath(node: DOMNode | optimized: boolean)
Returns the xpath (string) for the node supplied.
DOMPath.xpath(element);
DOMPath.xpath(element, true);