cd /news/ai-tools/3-ways-i-use-ai-to-auto-generate-pla… · home topics ai-tools article
[ARTICLE · art-127930] src=dev.to ↗ pub= topic=ai-tools verified=true sentiment=↑ positive

3 Ways I Use AI to Auto-Generate Playwright Locators

A test automation engineer with 18 years of experience described three ways he uses AI to auto-generate and maintain Playwright locators, including feeding DOM snapshots to an LLM to suggest resilient selectors, generating flagged fallback locators when primary selectors break, and translating BDD-style natural language scenarios into first-draft Playwright code. He cautioned against silent self-healing that rewrites test suites, arguing AI should remove tedious locator selection work while leaving test design judgment to humans.

by read2 min views2 publishedSep 12, 2026

If you've maintained a Playwright suite for more than a few months, you know the real cost isn't writing tests — it's the constant locator rot. A designer tweaks a class name, a component gets refactored, and suddenly a dozen tests are red for reasons that have nothing to do with actual bugs.

After 18 years in test automation — including building enterprise-scale frameworks — I've found that AI-assisted locator generation is one of the highest-leverage places to bring LLMs into a QA workflow. Here are three approaches I actually use, not just demo-ware.

Instead of hand-picking a data-testid or fighting brittle CSS selectors, I feed a snapshot of the relevant DOM section to an LLM and ask it to propose the most resilient locator strategy — prioritizing accessible roles and text over implementation-specific attributes.

// Instead of guessing at a selector manually:
const button = page.locator('.btn.btn-primary.mt-2.submit-btn-v2');

// Ask an AI-assisted step to suggest something resilient:
const button = page.getByRole('button', { name: 'Submit Order' });

The win isn't that AI "knows" your app — it's that it consistently nudges you toward Playwright's built-in resilient locator patterns (getByRole, getByLabel, getByText) instead of the CSS-selector habits many teams fall back into under deadline pressure.

When a primary locator fails during a run, rather than failing the test outright, I use an AI step to analyze the current DOM and suggest a fallback locator that matches the original intent of the interaction — then logs the drift so a human confirms it later.

This isn't "magic self-healing" that silently rewrites your test suite (which I'd actually caution against — silent healing can mask real regressions). It's a flagged suggestion: "Your original locator broke; here's what looks like the same element now; confirm before I update the source."

For teams writing BDD-style scenarios ("User clicks the 'Add to Cart' button"), I use AI to translate that natural language directly into a first-draft Playwright locator + action, which a human then reviews and commits.

When the user clicks "Add to Cart"

becomes a suggested:

await page.getByRole('button', { name: 'Add to Cart' }).click();

This dramatically speeds up onboarding less-experienced testers into a Playwright framework, since they're writing intent, not fighting Playwright's API surface on day one.

The common thread across all three: AI isn't replacing test design judgment — it's removing the tedious, error-prone parts of locator selection so testers can focus on what to test, not how to write a selector.

I go deeper into all of this — plus patterns for AI-assisted test generation, flaky test triage, and building a production-grade framework end to end — in my book, Playwright Test Automation with AI.

── more in #ai-tools 4 stories · sorted by recency
── more on @playwright 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/3-ways-i-use-ai-to-a…] indexed:0 read:2min 2026-09-12 ·