Skip to content

Playwright Locator Not Found Fix

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Playwright Locator Not Found Fix. We cover key concepts, practical examples, and best practices.

Playwright throws TimeoutError: locator.click: Timeout 30000ms exceeded — the locator never finds the element on the page. Your selector string might be wrong, the element might not be loaded yet, or it's rendered differently than expected.

The Problem

# WRONG — fragile CSS selector on dynamic content
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()
    page.goto('https://example.com')
    page.click('div.content > div.row > button:nth-child(3)')
TimeoutError: locator.click: Timeout 30000ms exceeded.
waiting for locator("div.content > div.row > button:nth-child(3)")

The deeply nested CSS selector is fragile — any DOM change breaks it. The button might not be the third child, or the content row class might differ from expected.

Step-by-Step Fix

page.click('text=Save Changes')
page.click('text=Submit')

2. Use data-testid attributes

# HTML: <button data-testid="save-btn">Save</button>
page.click('[data-testid="save-btn"]')

3. Use get_by_role for accessibility-based selection

page.get_by_role('button', name='Save').click()
page.get_by_label('Email address').fill('user@example.com')
page.get_by_placeholder('Enter your name').fill('Alice')

4. Wait for the element before interacting

from playwright.sync_api import expect

page.goto('https://example.com')
expect(page.get_by_test_id('user-list')).to_be_visible()
page.get_by_test_id('user-row').first.click()

5. Use locator chaining for precise targeting

modal = page.get_by_test_id('confirm-modal')
modal.get_by_role('button', name='Delete').click()

Expected output:

✓ Page loaded successfully
✓ User list visible
✓ Delete button clicked in modal

Prevention Tips

  • Use get_by_test_id() with dedicated data-testid attributes
  • Use text selectors (text=) for buttons and links
  • Use get_by_role() for accessibility-friendly targeting
  • Chain locators instead of writing complex CSS selectors
  • Always wait with expect() before interacting with dynamic elements

Common Mistakes with locator error

  1. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  2. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  3. Using return to exit a function early instead of wrapping a pure value in the monad

These mistakes appear frequently in real-world PLAYWRIGHT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What's the best selector strategy in Playwright?

Use data-testid attributes for stable selectors that never change with styling. Use get_by_role() for buttons and inputs. Use get_by_text() for readable content. Avoid HTML structure-based selectors (div > span > button). Playwright's locator API is designed to be robust — use it instead of raw CSS.

Why does Playwright timeout but the element is visible?

The locator might be in an iframe, shadow DOM, or the element might match multiple nodes and Playwright requires an actionability check. Use page.frame_locator() for iframes, or use .first, .last, .nth() to disambiguate multiple matches.

How is Playwright's auto-wait different from Selenium's waits?

Playwright automatically waits for elements to be actionable before performing actions — it waits for visibility, stability, and enabled state. Selenium requires explicit WebDriverWait calls. This means Playwright tests are simpler to write but timeouts happen if elements never become actionable.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro