• Blog
  • Docs
  • Pricing
  • We’re hiring!
Log inSign up
khawjaahmad

khawjaahmad

playwright-selector-gen

MCP: Fetch URL DOM and generate unique Playwright selectors
Public
Like
playwright-selector-gen
Home
Code
2
README.md
H
main.ts
Environment variables
5
Branches
1
Pull requests
Remixes
History
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
Sign up now
Code
/
Code
/
Search
README.md

Playwright Selector Generator

Endpoint: https://playwright.val.run

AI-powered service that fetches any URL's rendered DOM via Lightpanda headless browser and generates Playwright-native locators. The AI model does all selector generation — zero algorithmic logic in the code.


Authentication

Every POST request requires the X-Api-Key header.

curl -X POST https://playwright.val.run \ -H "Content-Type: application/json" \ -H "X-Api-Key: your_endpoint_key" \ -d '{"url": "https://www.saucedemo.com"}'

Environment Variables

Env VarRequiredPurpose
X_API_KEYYesProtects the endpoint
ANTHROPIC_API_KEYYesZ.AI / Anthropic API key for the model
ANTHROPIC_URLNoAPI endpoint (default: https://api.z.ai/api/anthropic/v1/messages)
ANTHROPIC_MODELNoModel identifier (default: anthropic/claude-sonnet-4-20250514)
LPD_TOKENYesLightpanda cloud token (sign up)
LPD_REGIONNoLightpanda region: euwest (default) or uswest

How It Works

Every request goes through Lightpanda's headless browser. No raw fetch(), no fallbacks.

POST { url } + X-Api-Key
        │
        ▼
┌─ Lightpanda Cloud ────────────┐
│  puppeteer.connect() via CDP  │
│  WebSocket to Lightpanda      │
│  Full JS rendering            │
│  networkidle2 wait            │
│  page.content() → HTML        │
└──────────┬────────────────────┘
           │
           ▼
┌─ Parse DOM (cheerio) ─────────┐
│  Extract raw elements:        │
│  tag, attributes, outerHTML,  │
│  text content                 │
│                               │
│  0 elements? → 422 error      │
└──────────┬────────────────────┘
           │
           ▼
┌─ AI Model (Z.AI) ────────────┐
│  Receives raw DOM data.       │
│  Generates ALL locators.      │
│  Ranks by Playwright priority.│
│  Returns JSON.                │
└──────────┬────────────────────┘
           │
           ▼
     JSON response (200)

API

GET /

Health check.

{ "status": "ok", "service": "playwright-selector-gen" }

POST /

Headers:

HeaderRequiredDescription
X-Api-KeyYesEndpoint access key
Content-TypeYesapplication/json

Body:

FieldTypeRequiredDescription
urlstringYesPage URL to analyze
filterstring[]NoOnly return elements matching these tags

Response (200)

{ "url": "https://www.saucedemo.com", "totalElements": 8, "elements": [ { "index": 0, "tag": "input", "locators": [ { "method": "getByPlaceholder", "playwrightCode": "page.getByPlaceholder('Username')", "confidence": "high" }, { "method": "getByRole", "playwrightCode": "page.getByRole('textbox', { name: 'Username' })", "confidence": "high" }, { "method": "getByTestId", "playwrightCode": "page.getByTestId('username')", "confidence": "high" } ] } ] }

Locator Priority

The AI follows the official Playwright recommendation:

RankMethodUsed For
1getByRole()ARIA role + accessible name
2getByLabel()Form fields with associated <label>
3getByPlaceholder()Inputs with placeholder text
4getByText()Visible text content
5getByAltText()Images with alt attribute
6getByTitle()Elements with title attribute
7getByTestId()data-testid / data-test / data-cy
8locator() CSSShort CSS only — absolute last resort
9locator() XPathOnly for truly complex DOM traversal

Each element gets up to 3 locators ranked best → fallback. Deep nested selectors are never generated.


Error Responses

StatusMeaning
401Missing or invalid X-Api-Key header
400Missing url in request body
405Method not allowed (use POST)
422No elements found after rendering
500Missing env vars / AI API error
502Lightpanda could not fetch the URL
Code
README.md
H
main.ts
FeaturesVersion controlCode intelligenceCLIMCP
Use cases
TeamsAI agentsSlackGTM
DocsShowcaseTemplatesNewestTrendingAPI examplesNPM packages
PricingNewsletterBlogAboutCareers
We’re hiring!
Brandhi@val.townStatus
X (Twitter)
Discord community
GitHub discussions
YouTube channel
Bluesky
Open Source Pledge
Terms of usePrivacy policyAbuse contact
© 2026 Val Town, Inc.