top of page

Synchronization In Selenium WebDriver


While executing a test case or test cases sometimes test execution speed may not match with your application process speed. Because of this test execution will get failed. To avoid this we have to determine this time gap and manage this time gap which is called as Synchronization.

When two or more components work together to perform any action at the same pace, the process is called synchronization. In Selenium, there should be synchronization between selenium script, web app and the script execution speed. When you tend to locate an element in your script which is yet to load on the page, selenium will throw you ‘ElementNotVisibleException’ message.

Types of Selenium Waits

  • Implicit Wait

  • Explicit Wait

  • Thread.Sleep() method

  • Fluent Wait

Let us understand each one of these in-depth.

Implicit Wait :

1. It directs the selenium webdriver to wait for a certain measure of time before throwing an exception.

2. It is declared globally, which means it is always available for all the web elements throughout the driver instance.

3. The key point to note here is, unlike Thread.Sleep(), it does not wait for the complete duration of time.

4. In case it finds the element before the duration specified, it moves on to the next line of code execution, thereby reducing the time of script execution. This is why Implicit wait is also referred to as dynamic wait.

5. If it does not find the element in the specified duration, it throws ElementNotVisibleException.

Old Syntax : Before Selenium 4 . This one is deprecated.

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

Syntax : After Selenium 4 this is the syntax used.


Advantages Of Implicit Wait :

  1. Since it is declared globally code looks readable and optimized.

Disadvantage Of Implicit Wait:

  1. Implicit wait will hide the performance issues of the application.

Practical Example Of Implicit Wait

I have used for demo . You can try with any websites.

public class Demo1 {

	public static void main(String[] args) throws Interrupted Exception {
		WebDriver driver =new EdgeDriver();
		driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));//Implicit wait declared globally.
		driver.findElement(By.xpath("//ul[@class='fswTabs latoBlack greyText'] //li[@data-cy='roundTrip']")).click();
		//Thread.sleep(3000);// Tried this. Not a good practice to use this wait.
		driver.findElement(By.xpath("//div[@class='makeFlex hrtlCenter'] [contains(.,'Chennai, India')]")).click();

Explicit Wait :

1. The Explicit wait is another one of the dynamic Selenium waits. It is classified into A. WebDriver wait and B. Fluent wait.

2.The Explicit Wait in Selenium is used to tell the Web Driver to wait for certain conditions (Expected Conditions) or maximum time exceeded before throwing “ElementNotVisibleException” exception.

3. It is an intelligent kind of wait, but it can be applied only for specified elements. In a scenario where you do not know the amount of time to wait for, this explicit wait comes in handy.

Types of Expected Conditions:

Below are the few types of expected conditions commonly used as you perform automation testing with Selenium.

  • visibilityOfElementLocated()- Verifies if the given element is present or not

  • alertIsPresent()- Verifies if the alert is present or not.

  • elementToBeClickable()- Verifies if the given element is present/clickable on the screen

  • textToBePresentInElement()- Verifies the given element have the required text or not

  • titles()- Verify the condition wait for a page that has a given title

There are many more expected conditions available, which you can refer through the Selenium official page.

Old Syntax : Before Selenium 4. This one is deprecated.

WebDriverWait wait = new WebDriverWait(driver,10);  wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".classlocator")));

New Syntax: After Selenium 4 this is the syntax used.

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

To use Explicit Wait in test scripts, import the following packages into the script.


Then, Initialize A Wait Object using WebDriverWait Class. WebDriverWait is a class and to access all the methos available in it an object wait is created. It takes 2 arguments . One is driver and another one the time in secs. Basic Java knowledge will be helpful in understanding these syntaxes.

WebDriverWait wait = new WebDriverWait(driver,30);

Advantages Of Explicit Wait

  1. It is applied only wherever required. So no performance issues.

Disadvantage of Explicit Wait

  1. More code.

Practical Example of Explicit Wait

Don't get confused with the code snippet shared below if you are a beginner. Just focus where Explicit wait is applied. By automating many demo websites will understand where Explicit wait is applied.

public class demo2 {

	public static void main(String[] args) throws InterruptedException {
		WebDriver driver =new EdgeDriver(); //did not download the drivers. Added the dependency selenium manager version 4.6.0. in pom.xml file. It was in beta phase while I was writing this blog.
		WebDriverWait w=new WebDriverWait(driver,Duration.ofSeconds(30));
		JavascriptExecutor js=(JavascriptExecutor)driver;
		driver.findElement(By.xpath("//button[@aria-label='Close Welcome Banner']")).click();
		w.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//mat-select[@role='combobox")));//Explicit wait applied for a particular element which is causing issues.
	Select dropdown=new Select(driver.findElement(By.xpath("//mat-select[@role='combobox']")));

Thread.Sleep() :

1.It does not listen to the dome

2. Even if the application gets opened it waits till the time specified . Hence time is wasted.

3. Not preferred in real time frameworks.



Fluent Wait :

1.It finds the web element repeatedly at regular intervals of time will the object gets found.

2.Fluent wait and WebDriver wait both are classes that implement wait interface.

3.While using Fluent Wait, it is possible to set a default polling period as needed. The user can configure the wait to ignore any exceptions during the polling period.

4.Fluent waits are also sometimes called smart waits because they don’t wait out the entire duration defined in the code.

5.Very rarely Fluent waits are used in real time.

Old Syntax : Before Selenium 4. This one is deprecated.

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)   .withTimeout(30, TimeUnit.SECONDS).pollingEvery(5, TimeUnit.SECONDS)   .ignoring(NoSuchElementException.class);

New Syntax : After Selenium 4 this is the syntax

Wait<WebDriver> fluentWait = new FluentWait<WebDriver>(driver)         .withTimeout(Duration.ofSeconds(30))         .pollingEvery(Duration.ofSeconds(5))         .ignoring(NoSuchElementException.class);

Differences between Implicit And Explicit Wait


In this blog we saw what is Synchronization concept in Selenium and what are the different types of waits available in selenium. In general it is recommended to use Explicit wait in our frameworks. Cypress has inbuilt wait mechanism. This is one of the major difference between Selenium and Cypress. It is said that if you want to drastically stabilize your test automation don't use the combination of both implicit and explicit waits in Selenium. Hope you find this blog helpful.

20 views2 comments

Recent Posts

See All
bottom of page