XPath to CSS Selector Converter
xpath-to-css is a JavaScript utility package designed to translate XPath expressions into equivalent CSS selectors. Originally conceived in Python for the `cssify` project by santiycr and later adapted to a JavaScript gist by Dither, this package by Jonathan Svenheden provides an npm-published, ES2015 and CommonJS-compatible solution. It is currently at version 1.2.0, with a release cadence that is likely infrequent, reflecting its stable utility nature rather than active feature development, given its last update was over two years ago. Its core differentiation lies in providing a readily accessible and battle-tested conversion tool for a specific web scraping and automation need, handling various common XPath patterns like ID, class, attribute, and positional selectors. Developers leverage it to bridge between systems that prefer CSS selectors (e.g., Playwright, Puppeteer) and sources that provide XPath (e.g., some browser developer tools, legacy systems).
Common errors
-
TypeError: xPathToCss is not a function
cause Attempting to import `xPathToCss` as a named export (`import { xPathToCss } from 'xpath-to-css';`) when it is a default export, or using incorrect CommonJS destructuring.fixFor ES Modules, use `import xPathToCss from 'xpath-to-css';`. For CommonJS, use `const xPathToCss = require('xpath-to-css');`. -
Error: Unsupported XPath expression: //some/complex/xpath[contains(text(), 'value')]/parent::*
cause The provided XPath expression uses features or patterns that the `xpath-to-css` library cannot translate into a CSS selector. Not all XPath constructs have direct CSS equivalents (e.g., parent traversal, complex text matching functions).fixSimplify the XPath expression if possible to use only commonly supported features (IDs, classes, attributes, direct child/descendant, position). If simplification is not feasible, consider manually crafting a CSS selector or using alternative element location strategies for the problematic part. Verify if the XPath expression is valid and if a CSS equivalent can exist.
Warnings
- gotcha Not all advanced or complex XPath expressions can be directly translated into equivalent CSS selectors. The utility might return an error or an incomplete selector for XPath features like parent traversal (`..`), preceding-sibling, or complex text content matching not based on `contains()`.
- gotcha The conversion process is synchronous. While generally fast for typical XPath expressions, for applications requiring high performance or processing a very large number of extremely complex XPath expressions, this might introduce blocking delays.
- gotcha The package appears to be in maintenance mode, with its last update over two years ago and no recent releases. While stable for its current functionality, new XPath features or complex edge cases introduced in newer browser standards may not be supported or actively developed.
Install
-
npm install xpath-to-css -
yarn add xpath-to-css -
pnpm add xpath-to-css
Imports
- xPathToCss
import { xPathToCss } from 'xpath-to-css';import xPathToCss from 'xpath-to-css';
- xPathToCss (CommonJS)
const { xPathToCss } = require('xpath-to-css');const xPathToCss = require('xpath-to-css');
Quickstart
import xPathToCss from 'xpath-to-css'; const xPath = '//div[@id="foo"][2]/span[@class="bar"]//a[contains(@class, "baz")]//img[1]'; const css = xPathToCss(xPath); console.log(css); // => 'div#foo:nth-of-type(2) > span.bar a[class*=baz] img:first-of-type' const simpleXPath = '//h1'; const simpleCss = xPathToCss(simpleXPath); console.log(simpleCss); // => 'h1' const attributeXPath = '//input[@type="text"]'; const attributeCss = xPathToCss(attributeXPath); console.log(attributeCss); // => 'input[type="text"]'