Why Synchronization Matters

Synchronization in Selenium WebDriver ensures that the automation script interacts with web elements at the appropriate time.

It helps prevent timing-related exceptions such as:

Selenium WebDriver provides three synchronization techniques:

Advertisement

Implicit Wait

An Implicit Wait is a global wait that instructs WebDriver to wait for a specified amount of time before throwing an exception.

It applies to the entire WebDriver session.

Example

 
public class ImplicitWaitExample {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        WebDriver driver = new ChromeDriver();

        // Set implicit wait to 10 seconds
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        // Rest of the automation code

        driver.quit();
    }
}
 

Once the implicit wait is set, WebDriver waits up to the specified timeout whenever it tries to locate an element.

Drawback

Implicit Wait is applied globally.

Every element lookup waits for the configured timeout before throwing an exception, even when such waiting is unnecessary.


Explicit Wait

An Explicit Wait waits for a specific condition or element.

It uses:

  • WebDriverWait
  • ExpectedConditions

Example

 
// Wait up to 10 seconds until the element becomes clickable

WebDriverWait wait = new WebDriverWait(driver, 10);

wait.until(ExpectedConditions.elementToBeClickable(By.id("element_id")));
 

The script waits only until the specified condition is satisfied.

Once the condition becomes true, execution continues immediately.

Unlike Implicit Wait, Explicit Wait is targeted only at the required element or event.


Fluent Wait

A Fluent Wait defines:

  • Maximum wait time
  • Polling frequency
  • Exceptions to ignore while waiting

Instead of continuously waiting, it checks for the element at regular intervals until:

  • The element is found, or
  • The timeout expires

Example

 
Wait<WebDriver> wait = new FluentWait<>(driver)

        .withTimeout(Duration.ofSeconds(10))

        .pollingEvery(Duration.ofMillis(500))

        .ignoring(Exception.class);
 

Explanation

withTimeout()

Specifies the maximum waiting time.

pollingEvery()

Specifies how often Selenium checks for the condition.

ignoring()

Specifies which exceptions should be ignored while polling.


When is Fluent Wait Useful?

Fluent Wait is useful when an element appears at unpredictable intervals.

For example, the element may appear after:

  • 10 seconds
  • 20 seconds
  • 30 seconds

Instead of checking continuously, Selenium checks after every polling interval until the timeout expires.


Why Prefer Explicit Wait Over Fluent Wait?

Explicit Wait is generally preferred because:

  • It waits only until the required condition is satisfied.
  • Execution continues immediately once the element is found.
  • It is simpler to implement.

The disadvantage of Fluent Wait is that repeated polling can consume the entire configured timeout.

Structurally:

  • Wait is an interface.
  • FluentWait is a class.
  • WebDriverWait is built on top of FluentWait and is the standard choice for most Selenium projects.

Waiting Until the Page Loads

To wait until a page has completely loaded, use WebDriverWait together with ExpectedConditions.

One commonly used condition is titleIs().

Example:

 
WebDriverWait wait = new WebDriverWait(driver, 10);

wait.until(ExpectedConditions.titleIs("Expected Page Title"));
 

Once the page title matches the expected value, Selenium continues execution.

ExpectedConditions also provides many other waiting conditions.


Thread.sleep()

The use of Thread.sleep() in Selenium automation should be minimized or avoided whenever possible.

Thread.sleep() creates a hard pause.

The script waits for the entire specified duration even if the required element becomes available earlier.

This leads to:

  • Slower execution
  • Unnecessary waiting
  • Less reliable automation

Instead, use:

  • Implicit Wait
  • Explicit Wait
  • Fluent Wait

These are condition-based waits.


setSpeed() vs Thread.sleep()

Both methods introduce delays, but they behave differently.

setSpeed()

  • Selenium-specific method.
  • Adds a delay between every Selenium command.
  • Affects all subsequent Selenium commands.

Thread.sleep()

  • Java method from java.lang.Thread.
  • Stops the entire program for the specified duration.
  • Nothing executes during this pause.

Difference Between setSpeed() and Thread.sleep()

setSpeed()

  • Provided by Selenium.
  • Adds delay between Selenium commands.
  • Affects all subsequent commands.

Thread.sleep()

  • Provided by Java (java.lang.Thread).
  • Completely pauses execution.
  • Applies only at the location where it is called.

FAQs

1. What is synchronization in Selenium?

Synchronization ensures that Selenium interacts with web elements at the correct time.

It prevents exceptions such as:

  • Element Not Found
  • Element Not Clickable
  • Stale Element Reference

Selenium provides:

  • Implicit Wait
  • Explicit Wait
  • Fluent Wait

2. What is an Implicit Wait?

An Implicit Wait is a global timeout applied to every element lookup throughout the WebDriver session.

Example:

 
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 

3. What is an Explicit Wait?

An Explicit Wait waits for a specific element or condition.

It uses:

  • WebDriverWait
  • ExpectedConditions

Example:

 
wait.until(ExpectedConditions.elementToBeClickable(locator));
 

4. What is a Fluent Wait?

A Fluent Wait allows you to configure:

  • Maximum timeout
  • Polling interval
  • Ignored exceptions

It repeatedly checks for the element until it is found or the timeout expires.


5. Why is Explicit Wait preferred over Fluent Wait?

Explicit Wait:

  • Waits only until the condition becomes true.
  • Continues execution immediately.
  • Is easier to implement.

Fluent Wait performs repeated polling and may consume the entire configured timeout.


6. Which wait is commonly used for page loading?

Use WebDriverWait together with:

 
ExpectedConditions.titleIs("Expected Page Title")
 

The script proceeds after the page title matches the expected value.


7. Why should Thread.sleep() be avoided?

Thread.sleep() creates a fixed pause.

It:

  • Slows test execution.
  • Does not adapt to application speed.
  • Waits even if the element is already available.

Condition-based waits are faster and more reliable.


8. What is the difference between setSpeed() and Thread.sleep()?

setSpeed()

  • Selenium method.
  • Delays every Selenium command.

Thread.sleep()

  • Java method.
  • Completely pauses program execution for the specified duration.