npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

dom-to-locator

v1.0.0

Published

Generate Playwright locators from live DOM elements

Readme

dom-to-locator

Generate Playwright locator strings from live DOM elements.

What it does

Given any DOM element, produces the best Playwright locator string (e.g. getByRole('button', { name: 'Submit' })) using the same scoring and uniqueness algorithm that powers Playwright's Inspector, Codegen, and recorder tools.

Strategies are tried in priority order: test ID, ARIA role + name, placeholder, label, alt text, text content, title, CSS ID, tag name — falling back to nth-index or full CSS path when nothing else is unique.

Install

pnpm add dom-to-locator

Usage

import { generateLocator, generateLocators, asLocator } from "dom-to-locator";

// Best locator for an element
generateLocator(element);
// => "getByRole('button', { name: 'Submit' })"

// Multiple ranked variants
generateLocators(element);
// => ["getByTestId('submit-btn')", "getByRole('button', { name: 'Submit' })"]

// Custom test ID attribute
generateLocator(element, { testIdAttributeName: "data-cy" });

// Python / Java / C#
generateLocator(element, { language: "python" });
// => 'get_by_role("button", name="Submit")'

// Scoped to a subtree
generateLocator(element, { root: containerElement });

// Format an internal selector string (no DOM needed)
asLocator("javascript", 'internal:role=button[name="Submit"i]');
// => "getByRole('button', { name: 'Submit' })"

API

generateLocator(element, options?): string

Returns the single best Playwright locator for the given element.

generateLocators(element, options?): string[]

Returns multiple locator variants, ranked from best to worst.

generateInternalSelector(element, options?): string

Returns the raw internal selector string before language-specific formatting.

asLocator(language, selector): string

Converts an internal selector string to a language-specific locator. No DOM required.

asLocators(language, selector): string[]

Converts an internal selector string to multiple language-specific locator variants.

Options

| Option | Type | Default | Description | | --------------------- | --------------------- | --------------- | ----------------------------------------------------------------- | | testIdAttributeName | string | 'data-testid' | HTML attribute used for test IDs | | language | Language | 'javascript' | Target language: 'javascript', 'python', 'java', 'csharp' | | root | Element \| Document | document | Scope locator generation to a subtree |

How it works

The pipeline has two stages:

1. DOM element to internal selector (browser-side, requires live DOM)

The selectorGenerator inspects the target element and builds candidate selectors using every available strategy (ARIA role, test ID, text, CSS, etc.). Each candidate is scored — lower is better — and tested for uniqueness by querying the DOM. If a selector matches exactly one element (the target), it wins. When no single selector is unique, the generator composes nested selectors (parent >> child) or appends nth= as a last resort.

2. Internal selector to locator string (isomorphic, no DOM needed)

The locatorGenerators module parses the internal selector syntax (internal:role=button[name="Submit"i]) and routes it through a language-specific factory to emit idiomatic code (getByRole('button', { name: 'Submit' }) for JavaScript, get_by_role("button", name="Submit") for Python, etc.).

Scoring priority

| Score | Strategy | Locator | | ---------- | ----------------------- | -------------------------------------- | | 1 | Test ID | getByTestId(...) | | 2 | Other data-test* attrs | locator('[data-test=...]') | | 100 | ARIA role + name | getByRole('button', { name: '...' }) | | 120 | Placeholder | getByPlaceholder(...) | | 140 | Label | getByLabel(...) | | 160 | Alt text | getByAltText(...) | | 180 | Text content | getByText(...) | | 200 | Title | getByTitle(...) | | 500 | CSS ID | locator('#id') | | 510 | ARIA role (no name) | getByRole('button') | | 10,000 | nth-index | .nth(N) | | 10,000,000 | CSS fallback | locator('div > span.class') |

Provenance

This package extracts and bundles the locator generation subsystem from Playwright (Apache-2.0). The original code lives across two layers:

  • packages/injected/src/selectorGenerator.ts + supporting modules (roleUtils.ts, domUtils.ts, selectorUtils.ts, selectorEvaluator.ts, roleSelectorEngine.ts) — the DOM-side candidate generation and uniqueness testing
  • packages/playwright-core/src/utils/isomorphic/ (locatorGenerators.ts, selectorParser.ts, cssParser.ts, cssTokenizer.ts, stringUtils.ts) — the isomorphic selector parsing and locator formatting

The main adaptation is host.ts, a 220-line shim that replaces Playwright's 1,800+ line InjectedScript class, registering only the selector engines that locator generation actually exercises.

License

BSD-3-Clause. Derived from Playwright (Apache-2.0).