JSPM

cheerio-get-css-selector

1.1.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 117
  • Score
    100M100P100Q80848F
  • License MIT

gets a unique selector for this element

Package Exports

  • cheerio-get-css-selector

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (cheerio-get-css-selector) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

cheerio-get-css-selector

Adds one extra function to cheerio: get a unique css selector.

When searching through the ancestors while building the selector the algorithm with stop at the closest element that has an id.

How to use:

Install the package:

npm install cheerio-get-css-selector

Init the library and pass it your cheerio object:

require('get-unique-selector').init($);

Use it as a method of cheerio objects:

$element.getUniqueSelector();

Example

<html> 
  <body> 
    <div id="my-div"> 
      <span> 
        <span class="test-class"></span>
        <span></span>
      </span> 
    </div> 
  </body> 
</html>
var cheerio = require('cheerio');
var GetUniqueSelector = require('get-unique-selector');

var $ = cheerio.load('<html><body><div id="my-div"><span><span class="test-class"></span></span></div></body></html>');
GetUniqueSelector.init($);

var $element = $('.test-class');

console.log($element.getUniqueSelector()); // outputs:#my-div > span > span:first-child