How do Playwright's locators and auto-waiting change the way you write Python UI tests compared with Selenium-style explicit waits?
Answer
A Playwright locator is re-resolved when it is used and participates in retryable actions and assertions. Before an action such as click, Playwright waits for relevant actionability conditions such as visibility, stability, event reception and enabled state; web-first assertions retry until their condition is met. Prefer user-facing locators such as role, label or test id over brittle DOM chains, and do not replace Playwright's waiting model with `time.sleep()` or arbitrary timeouts unless diagnosing a specific problem.
Strong answer includes
- explains locator re-resolution and actionability rather than saying only that Playwright waits automatically
- prefers semantic user-facing locators over brittle CSS or XPath structure
- uses retrying assertions and avoids fixed sleeps as normal synchronization
Practical example
For a Save button,
page.get_by_role("button", name="Save").click()lets Playwright wait until that single button is actionable. A following
expect(page.get_by_text("Saved")).to_be_visible()retries the assertion; adding
time.sleep(2)between the two usually makes the test slower and less deterministic.