Playwright Locator Not Found Fix
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
1. Use text selectors for buttons and links
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 dedicateddata-testidattributes - 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
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro