how auto wait works in Playwright
Let’s see how auto wait works in Playwright
⚙️ The Problem Auto-Wait Solves
In web automation, the page is dynamic — elements appear, disappear, or become interactive at unpredictable times. Without waiting correctly, your test will fail with:
- ❌
NoSuchElementException - ❌
ElementNotInteractableException - ❌
StaleElementReferenceException
So every automation framework needs a waiting strategy.
🧩 Selenium’s Wait Model
1. Manual Waiting
In Selenium, you often have to explicitly wait:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("submit")));
button.click();
✅ Works, but you need to manually specify:
- Which element to wait for
- How long to wait
- What condition to wait for (visible, clickable, etc.)
If you forget or use the wrong condition → flaky tests. Selenium is “dumb” about element states — it just follows your instructions.
⚙️ Playwright’s Auto-Wait Model
Playwright has intelligent, built-in waiting.
It automatically waits for the “right moment” before performing most actions — no explicit wait() needed.
Example:
await page.click('#submit');
That single line does all of this automatically:
- ✅ Waits for the element
#submitto appear in the DOM - ✅ Waits for it to be visible (not hidden via CSS)
- ✅ Waits for it to be enabled (not disabled)
- ✅ Waits for it to be stable (not moving or re-rendering)
- ✅ Scrolls it into view if needed
- ✅ Then performs the click
If any of these fail within the default timeout (30 seconds), it throws a descriptive error.
🧠 Real Example: Auto-Wait in Action
Selenium:
driver.findElement(By.id("loginBtn")).click(); // might fail if page is still loading
Playwright:
await page.click('#loginBtn'); // waits automatically for element to be ready
No need for Thread.sleep(), wait.until(), or ExpectedConditions.
🕵️ Behind the Scenes — Why Playwright Can Do This
Playwright uses the browser’s DevTools Protocol (CDP) (same low-level API used by Chrome DevTools).
This gives it deep insight into:
- DOM events (mutation, visibility)
- Network requests and responses
- Rendering lifecycle (paint, layout, animation frames)
So Playwright knows when the element is truly stable and ready. Selenium (WebDriver protocol) doesn’t have that visibility.
🧮 Summary Comparison
| Feature | Selenium | Playwright |
|---|---|---|
| Waiting model | Manual (explicit, implicit, fluent waits) | Automatic (auto-wait built in) |
| Waits for visible? | Only if you specify | ✅ Built-in |
| Waits for enabled? | Only if you specify | ✅ Built-in |
| Waits for stable (no animation)? | ❌ No | ✅ Yes |
| Smart retry logic | ❌ No | ✅ Yes |
| DOM awareness | Limited | Deep via CDP |
| Test flakiness | Higher | Much lower |
✅ Summary
| Selenium | Playwright |
|---|---|
| “You tell me when to wait.” | “I’ll wait intelligently for you.” |
Playwright auto-waits for elements to be actionable before interacting — drastically reducing test flakiness and the need for sleep() or waitUntil() calls.