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

playwright-selector-generator

v0.0.0

Published

Playwright's official selector generator, extracted as a standalone browser-side module. Same code that powers `playwright codegen` and the trace viewer.

Readme

playwright-selector-generator

npm version npm downloads

Playwright's official selector generator, extracted via a Vite plugin as a standalone browser-side module. This is the exact code that powers playwright codegen and the trace viewer.

Install

npm install playwright-selector-generator

No runtime dependencies. The Playwright InjectedScript is pre-bundled at build time.

Usage

generateLocator(element, options?)

Generate the best Playwright locator for a DOM element:

import { generateLocator } from 'playwright-selector-generator';

document.addEventListener('click', (e) => {
  const locator = generateLocator(e.target);
  console.log(locator);
  // => "getByRole('button', { name: 'Submit' })"
  // => "getByTestId('my-id')"
  // => "getByLabel('Email')"
  // => "locator('#sidebar .nav-item')"
});

Options:

| Option | Type | Default | Description | | ----------------- | --------------------- | --------------- | --------------------------------------------------------- | | testIdAttribute | string | 'data-testid' | Attribute for test IDs | | root | Element \| Document | document | Scope queries to a subtree | | lang | string | 'javascript' | Output language: javascript, python, java, csharp |

generateSelectors(element, options?)

Get all selector alternatives for an element, ranked by quality. Returns internal-format strings — convert with selectorToLocator():

import {
  generateSelectors,
  selectorToLocator,
} from 'playwright-selector-generator';

const selectors = generateSelectors(button);
// [
//   'internal:testid=[data-testid="submit-btn"s]',
//   'internal:role=button[name="Submit"i]',
//   'css=#submit',
// ]

// Convert to any language:
selectors.map((s) => selectorToLocator(s, 'python'));
// [
//   "get_by_test_id('submit-btn')",
//   "get_by_role('button', name='Submit')",
//   "locator('#submit')",
// ]

selectorToLocator(selector, lang?)

Convert an internal Playwright selector to a locator string:

import { selectorToLocator } from 'playwright-selector-generator';

selectorToLocator('internal:role=button[name="OK"i]');
// => "getByRole('button', { name: 'OK' })"

selectorToLocator('internal:role=button[name="OK"i]', 'python');
// => "get_by_role('button', name='OK')"

selectorToLocator('internal:role=button[name="OK"i]', 'java');
// => "getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName(\"OK\"))"

Vite plugin

For advanced usage, a Vite plugin is included that serves Playwright's InjectedScript as a virtual module. This is useful if you need direct access to the InjectedScript class or are running it on an updated playwright package.

// vite.config.ts
import playwrightInjected from 'playwright-selector-generator/vite';

export default defineConfig({
  plugins: [playwrightInjected()],
});

Then import in your code:

import { InjectedScript } from 'virtual:playwright-injected';

TypeScript: Add the type declarations via either a triple-slash reference:

/// <reference types="playwright-selector-generator/virtual-playwright-injected" />

Or in tsconfig.json:

{
  "compilerOptions": {
    "types": ["playwright-selector-generator/virtual-playwright-injected"]
  }
}

The Vite plugin requires playwright-core as a peer dependency (to extract the InjectedScript at build time).

How it works

At package build time, a Vite plugin extracts Playwright's InjectedScript from playwright-core/lib/generated/injectedScriptSource.js and bundles it alongside the locatorGenerators utilities into a single self-contained ES module. No runtime eval, no playwright-core dependency for consumers.