Home  Playwright   How auto wa ...

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:

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:

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:

  1. ✅ Waits for the element #submit to appear in the DOM
  2. ✅ Waits for it to be visible (not hidden via CSS)
  3. ✅ Waits for it to be enabled (not disabled)
  4. ✅ Waits for it to be stable (not moving or re-rendering)
  5. ✅ Scrolls it into view if needed
  6. ✅ 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:

So Playwright knows when the element is truly stable and ready. Selenium (WebDriver protocol) doesn’t have that visibility.


🧮 Summary Comparison

FeatureSeleniumPlaywright
Waiting modelManual (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 awarenessLimitedDeep via CDP
Test flakinessHigherMuch lower

✅ Summary

SeleniumPlaywright
“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.


Published on: Oct 17, 2025, 12:44 AM  
 

Comments

Add your comment