Skip to main content

playwright-pom-agent

Generates Playwright Python Page Object Model classes from a live page, using an LLM to propose locators and a live verification loop to make sure every locator it writes is actually unique on the page before it's used.

Data sent to the LLM provider

Generation sends the page's accessibility tree (roles, visible text, structure) and a small test-id attribute inventory (tag + attribute value + a short text snippet) to your configured LLM provider (Anthropic or OpenAI). It does NOT read cookies, local/session storage, or form field values. If a page visibly displays sensitive info (names, account numbers, etc.), that text is included in what's sent - review before running against such pages. The CLI prints this notice on every run; it doesn't block execution.

Locator priority

  1. get_by_test_id() (data-testid, or a custom attribute via --test-id-attribute)
  2. get_by_role()
  3. get_by_label()
  4. get_by_placeholder()
  5. get_by_text()
  6. get_by_alt_text() / get_by_title()
  7. #id CSS selector
  8. [name="..."] CSS selector
  9. Generic CSS / XPath (last resort)

Every candidate is verified with .count() == 1 on the live page before being written to the generated file. Ambiguous or unresolved elements are retried (default: 3 attempts, via .filter(has_text=...) or parent scoping) before being emitted with an # UNVERIFIED comment rather than looping forever.

Dynamic UI state (modals, dropdowns, accordions)

Only the DOM present at inspection time is visible. This tool does not auto-crawl interactions. To document elements behind an interaction:

  • CLI: pass --setup-script path/to/setup.py, a file defining def setup(page): ... that runs after navigation and before inspection (e.g. click to open a modal).
  • Programmatic API: drive page into the desired state yourself before calling POMAgent.generate(page).

Generated files

Generated POM files start with an # AUTO-GENERATED ... DO NOT EDIT header. Re-running generation overwrites the file completely - put custom logic in a subclass, not in the generated file.

CLI

Set ANTHROPIC_API_KEY or OPENAI_API_KEY (matching --provider) before running - the CLI also accepts --api-key directly, but the env var is preferred since CLI arguments can leak via shell history and process listings.

export ANTHROPIC_API_KEY=sk-...
pom-agent generate --url https://example.com/login \
  --out example_login_page.py \
  --test-id-attribute data-pw \
  --provider anthropic

Programmatic

from playwright.sync_api import sync_playwright
from playwright_pom_agent import POMAgent

with sync_playwright() as p:
    page = p.chromium.launch().new_page()
    page.goto("https://example.com/login")

    agent = POMAgent(provider="anthropic")
    code = agent.generate(page, out_path="login_page.py")

Testing

uv pip install -e ".[dev]"
pytest tests/

tests/test_fixtures/ runs against local HTML fixtures in tests/fixtures/. tests/test_demo/test_demo_url.py is a reference suite that exercises the whole tool against a real site - https://demo.realworld.show/, a public "Conduit" demo app with no data-testids or <label>s anywhere, so it genuinely tests the role/placeholder fallback tiers instead of the tier-1 shortcut most fixtures hit. It covers locator resolution across the login, register, settings, and editor pages, plus full programmatic and CLI generation runs that actually log in with the generated code - those runs write their generated POM classes to tests/test_demo/pages/ (overwritten on each run) so you can open real, working, generated output rather than just reading assertions. Use it as a template for pointing this tool at your own app (swap BASE_URL/TEST_EMAIL/TEST_PASSWORD, or set the REALWORLD_* env vars it reads).

These tests are marked live and require internet access:

pytest tests/ -m "not live"   # skip the live/network tests
pytest tests/ -m live         # run only the live tests

One test in that file makes a real, billed LLM call and is opt-in only - it won't run just because ANTHROPIC_API_KEY/OPENAI_API_KEY happen to be set in your shell:

RUN_LIVE_LLM_TESTS=1 ANTHROPIC_API_KEY=sk-... pytest tests/test_demo/test_demo_url.py -k real_llm

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

playwright_pom_agent-0.1.0.tar.gz (71.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

playwright_pom_agent-0.1.0-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

Details for the file playwright_pom_agent-0.1.0.tar.gz.

File metadata

  • Download URL: playwright_pom_agent-0.1.0.tar.gz
  • Upload date:
  • Size: 71.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for playwright_pom_agent-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b6e59c704961c20a163b12f6e604a9558cebc924a514325d2e9f88bfa03b3e21
MD5 66ae538904e51134ed1fba476c6538dc
BLAKE2b-256 6b306be0fbe6af22a821e57f9cb01366d5c185141a14944a0bb4b3ce64b6b3a1

See more details on using hashes here.

Provenance

The following attestation bundles were made for playwright_pom_agent-0.1.0.tar.gz:

Publisher: publish.yml on VinayakaMayura/playwright-pom-agent

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file playwright_pom_agent-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for playwright_pom_agent-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5927e341e35e0dc18a187aee64e75ed87fa57f262e2eeb43416e7d0d76f68665
MD5 12facfb81a54fc181b36457e38b2e3ab
BLAKE2b-256 b065a75d8421f042d1aba5d05f972e579f7b2a9f00e2b404628fcabeb7905d5f

See more details on using hashes here.

Provenance

The following attestation bundles were made for playwright_pom_agent-0.1.0-py3-none-any.whl:

Publisher: publish.yml on VinayakaMayura/playwright-pom-agent

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page