JavaScript object that creates unique CSS selector for given element.
npm install css-selector-generatorJavaScript library for creating CSS selectors for a given DOM element or multiple DOM elements.
It also generates shorter selectors and is faster and/or more robust than many other libraries - see this comparison and select the best alternative for your use case.
Add the library to your project via NPM or Yarn.
``shell`
npm install css-selector-generatoror
yarn add css-selector-generator
Then include it in your source code:
`javascript`
import { getCssSelector, cssSelectorGenerator } from "css-selector-generator";
Simplest way to use it is to provide an element reference, without any options.
`html`
You can use either getCssSelector function that returns the first found selector:
`javascript`
getCssSelector(targetElement);
// ".myElement"
Or you can use cssSelectorGenerator function that returns a generator yielding all possible selectors, starting from the simplest ones, ending with the fallback selector:
`javascript`
[...cssSelectorGenerator(targetElement)];
// [".myElement", "div.myElement", "body .myElement"]
You can set options using second parameter for both functions. The options are described in the Options section.
`javascript
getCssSelector(targetElement, { includeTag: true });
// "div.myElement"
[...cssSelectorGenerator(targetElement, { maxResults: 2 })];
// [".myElement", "div.myElement"]
`
Typical example is to create a selector for any element that the user clicks on:
`javascript`
// track every click
document.body.addEventListener("click", function (event) {
// get reference to the element user clicked on
const element = event.target;
// get unique CSS selector for that element
const selector = getCssSelector(element);
// do whatever you need to do with that selector
console.log("selector", selector);
});
If you don't want to use this library with NPM, you can download it from the "build" folder and insert it to your HTML document directly. In this case, the library is wrapped in namespace CssSelectorGenerator. So the usage would look something like this:
`html`
If you want to use this library with Node, usually for testing, don't require it directly into the Node process. It will not work, because there's no window object and there are no elements to select. Instead, you have to add the library to the virtual window object. Here are instructions how to do it in JSDOM, other libraries will work in a similar way:
https://github.com/jsdom/jsdom/wiki/Don't-stuff-jsdom-globals-onto-the-Node-global
This library is written in TypeScript and includes type definitions. You can import the types directly:
`typescript
import { getCssSelector, cssSelectorGenerator } from "css-selector-generator";
import type {
CssSelectorGeneratorOptionsInput,
CssSelectorType,
} from "css-selector-generator/types/types.js";
const options: CssSelectorGeneratorOptionsInput = {
selectors: ["class", "id", "tag"],
blacklist: [".ignore-*"],
root: document.body,
};
const selector = getCssSelector(targetElement, options);
`
This library also allows you to create selector targeting multiple elements at once. You do that by calling the same function, but you provide an array of elements instead of single element:
`html`
`javascript`
getCssSelector([firstElement, secondElement]);
// ".bbb"
If it is not possible to construct single selector for all elements a standalone selector for each element will be generated:
`html`
`javascript`
getCssSelector([firstElement, secondElement]);
// "div, span"
getCssSelector determines the shortest CSS selector for parent -> child relationship, from the input Element until the Root Element.
If there is no _unique_ selector available for any of these relationships (parent -> child), a fallback of * will be used for this relationship.
#wrapper > * > div > .text
In some cases, this selector may not be unique (e.g. #wrapper > > div > ). In this case, it will fall back to an entire chain of :nth-child selectors like:
":nth-child(2) > :nth-child(4) > :nth-child(1) > :nth-child(12)"
- selectors
- root
- blacklist
- whitelist
- combineWithinSelector
- combineBetweenSelectors
- includeTag
- maxCombinations
- maxCandidates
- maxResults _(only applicable in cssSelectorGenerator)_useScope
-
You can choose which types of selectors do you want to use:
`html`
`javascript`
getCssSelector(targetElement, { selectors: ["class"] });
// ".myElement"
getCssSelector(targetElement, { selectors: ["tag"] });
// "div"
Order of selector types defines their priority:
`javascript`
getCssSelector(targetElement, { selectors: ["class", "tag"] });
// ".myElement"
getCssSelector(targetElement, { selectors: ["tag", "class"] });
// "div"
Valid selector types are:
- idclass
- tag
- attribute
- nthchild
- nthoftype
-
You can define root element, from which the selector will be created. If root element is not defined, document root will be used:
`html`
`javascript`
getCssSelector(targetElement);
// ".myRootElement > .myElement"
getCssSelector(targetElement, {
root: document.querySelector(".myRootElement"),
});
// ".myElement"
If you want to ignore some selectors, you can put them on the blacklist. Blacklist is an array that can contain either regular expressions, strings and/or functions.
In strings, you can use an asterisk (*) as a wildcard that will match any number of any characters.
Functions will receive a selector as a parameter. They should always return boolean, true if it is a match, false if it is not. Any other type of return value will be ignored.
`html`
`javascript`
getCssSelector(targetElement, { blacklist: [".firstClass"] });
// ".secondClass"
getCssSelector(targetElement, { blacklist: [".first*"] });
// ".secondClass"
getCssSelector(targetElement, { blacklist: [/first/] });
// ".secondClass"
getCssSelector(targetElement, {
blacklist: [(input) => input.startsWith(".first")],
});
// ".secondClass"
You can target selectors of any types using the blacklist.
`javascript`
getCssSelector(targetElement, {
blacklist: [
// ID selector
"#forbiddenId",
// class selector
".forbiddenClass",
// attribute selector
"[forbidden-attribute]",
// tag selector
"div",
],
});
Same as blacklist option, but instead of ignoring matching selectors, they will be prioritised.
`html`
`javascript`
getCssSelector(targetElement, { whitelist: [".secondClass"] });
// ".secondClass"
getCssSelector(targetElement, { whitelist: [".second*"] });
// ".secondClass"
getCssSelector(targetElement, { whitelist: [/second/] });
// ".secondClass"
If set to true, the generator will try to look for combinations of selectors within a single type (usually class names) to get better overall selector.
`html`
`javascript`
getCssSelector(targetElement, { combineWithinSelector: false });
// "body > :nth-child(1)" - in this case no single class name is unique
getCssSelector(targetElement, { combineWithinSelector: true });
// ".aaa.bbb"
This option is set to true by default. It can be set to false for performance reasons.
If set to true, the generator will try to look for combinations of selectors between various types (e.g. tag name + class name) to get better overall selector.
`html`
`javascript`
getCssSelector(targetElement, { combineBetweenSelectors: false });
// "body > :nth-child(1)" - in this case no single class name or tag name is unique
getCssSelector(targetElement, { combineBetweenSelectors: true });
// "div.aaa"
This option is set to true by default. It can be set to false for performance reasons.
This option will add tag selector type to every selector:
`html`
`javascript`
getCssSelector(targetElement, { includeTag: true });
// "div.myElement"
This is a performance optimization option that can help when trying to find a CSS selector within elements that contain large numbers of class names (e.g. because of frameworks that create atomic styles) or other attributes.
In such case, the number of possible combinations between class names can be too large (it grows exponentially) and can significantly slow down selector generation. In reality, if the selector is not found within first few combinations, it usually won't be found within the rest of combinations.
`javascript`
getCssSelector(targetElement, { maxCombinations: 100 });
Performance optimization option, similar to maxCombinations. This does limit a total number of selector candidates for each element.
You should use it in cases, when there are not too many class names and attributes, but they are numerous enough to produce large number of combinations between them.
`javascript`
getCssSelector(targetElement, { maxCandidates: 100 });
_Only applicable in the cssSelectorGenerator(). Not in the getCssSelector() function._
Limits the maximum number of yielded selectors.
By default, all possible selectors are yielded, starting from the simplest ones, ending with the fallback selector. In some cases, this may produce a very large number of selectors. To prevent performance issues, you should set a limit to the number of results.
You can also use it to get multiple selector options, so you can choose the one you like the most.
Let's say you have the following HTML code:
`html`
This will produce 2^n selectors, where n is the number of classes in the element (in case the class names produce unique selectors). The number of possible combinations grows exponentially:
`javascript`
const allSelectors = [
...cssSelectorGenerator(needleElement, { selectors: ["class"] }),
];
// [".aaa", ".bbb", ".ccc", ".aaa.bbb", ".aaa.ccc", ".bbb.ccc", ".aaa.bbb.ccc"]
That's why it's a good idea to limit the maximum number of results. Besides, their quality tends to decrease with the increasing complexity.
`javascript`
const fewSelectors = [
...cssSelectorGenerator(needleElement, {
selectors: ["class"],
maxResults: 5,
}),
];
// [".aaa", ".bbb", ".ccc", ".aaa.bbb", ".aaa.ccc"]
Experimental feature - _This will probably be turned on by default and the option will be removed, after I thoroughly evaluate that it produces valid selectors in all use cases._
If set to true and the root option is provided, the fallback selectors will be created relative to the root element using the :scope pseudo-class.
For example, if you have the following HTML structure:
`html`
If you generate the selector without the useScope option:
`javascript`
getCssSelector(needleElement, {
root: haystackElement,
useScope: false,
});
...it will produce this fallback selector:
``
:root > :nth-child(1) > :nth-child(1) > :nth-child(1) > :nth-child(1) > :nth-child(1)
... where the selectors correspond with these elements:
` But if you generate the selector with the ...it will produce this fallback selector: ... where the selectors correspond with these elements: If you found any bugs, if you have feature requests or any questions, please, either [file an issue on GitHub][1] or send me an e-mail at [riki@fczbkk.com][2] CSS Selector Generator is published under the [MIT license][3]. [1]: https://github.com/fczbkk/css-selector-generator/issues
:root ->
> :nth-child(1) ->
> :nth-child(1) ->
> :nth-child(1) ->
> :nth-child(1) ->
> :nth-child(1) ->
`useScope option:`javascript`
getCssSelector(needleElement, {
root: haystackElement,
useScope: true,
});:scope > :nth-child(1) > :nth-child(1)`
:scope ->
> :nth-child(1) ->
> :nth-child(1) ->
``Bug reports, feature requests and contact
License
[2]: mailto:riki@fczbkk.com?subject=CSSSelectorGenerator
[3]: https://github.com/fczbkk/css-selector-generator/blob/master/LICENSE