#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Selenium WebDriver

Synchronization Commands: Thread.sleep(), Implicit Wait, Explicit Wait

Thread.sleep()

Pauses execution for a fixed duration regardless of whether the page is ready, e.g. Thread.sleep(3000); — simple but wasteful and unreliable, generally discouraged.

Implicit Wait

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

Applies a default wait time across all element lookups for the whole driver session, polling repeatedly until the element appears or the timeout expires.

Explicit Wait

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("submit")));

Waits for a specific condition on a specific element, giving far more precise control than implicit waits — the generally recommended approach for reliable synchronization.

Why Scripts Fail Without Synchronization

Selenium sends commands far faster than a browser can load pages, run JavaScript or finish AJAX calls. If a script looks for a button before it exists, Selenium throws NoSuchElementException. If the button exists but is hidden or covered by a loader, ElementNotInteractableException or ElementClickInterceptedException appears instead. Waits close the gap between script speed and page readiness.

Each kind of wait handles this differently:

  • Thread.sleep always pauses the full time, even if the element was ready after a fraction of a second.
  • Implicit wait only affects findElement and findElements; it checks for presence in the DOM, not visibility or clickability.
  • Explicit wait repeatedly evaluates a condition, by default every half second, and returns as soon as the condition is true.

Fluent Wait and Useful Conditions

WebDriverWait is actually a ready-made fluent wait. When you need custom polling or want to ignore specific exceptions, build one directly:

Wait<WebDriver> wait = new FluentWait<>(driver)
        .withTimeout(Duration.ofSeconds(20))
        .pollingEvery(Duration.ofMillis(500))
        .ignoring(NoSuchElementException.class);

WebElement result = wait.until(d -> d.findElement(By.id("result")));

Commonly used conditions from ExpectedConditions include:

ConditionTypical use
elementToBeClickableButtons and links that are disabled until a form is valid
visibilityOfElementLocatedMessages and panels that appear after an action
invisibilityOfElementLocatedWaiting for a spinner or overlay to disappear
textToBePresentInElementLocatedStatus labels that change from "Processing" to "Done"
urlContainsConfirming navigation after login or submit
alertIsPresentJavaScript alerts that open after a delay

Worked Example: Waiting Out a Loading Spinner

A search page shows an overlay while results load. Clicking the first result too early hits the overlay. A reliable version looks like this:

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15));

driver.findElement(By.id("search")).sendKeys("laptop");
driver.findElement(By.id("go")).click();

wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector(".loader")));
WebElement first = wait.until(
        ExpectedConditions.elementToBeClickable(By.cssSelector(".result a")));
first.click();

wait.until(ExpectedConditions.urlContains("/product/"));

Every wait states what the script is waiting for, so a timeout message tells you exactly which step was slow.

Mistakes That Make Tests Flaky

  • Mixing implicit and explicit waits. The two can combine in unpredictable ways and lengthen timeouts. Pick explicit waits and leave the implicit wait at zero.
  • Sleeping "just to be safe". A few three-second sleeps across hundreds of tests add a lot of runtime and still fail on a slow day.
  • Waiting for presence when you need visibility. An element can be in the DOM but hidden.
  • Holding stale references. If the page re-renders, re-locate the element after the wait instead of reusing an old WebElement.
  • Huge timeouts. Very long waits hide real performance problems; use realistic limits.

A practical rule: put waits inside page-object methods, such as clickLogin(), so every test that uses them is synchronized automatically.

Ready to master real-world software testing?

Learn manual and automation testing hands-on with mentor-led sessions.

Explore Course