Site icon

Explicit Wait in Selenium WebDriver – Java (Synchronization)

 Must Read : Handle ElementNotVisibleException in Selenium Webdriver

                       Implicit Wait in Selenium WebDriver – Java (Synchronization)

Syntax:

// Create instance of WebDriverWait class 
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));
  
// Wait till the element becomes visible
 WebElement webElement=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("Enter xpath")));

Code for Explicit wait:

package TestCases;

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

public class ExplicitWaitTest {
	public WebDriver driver;

	@Test
	public void explicitWait() {
		// Create instance of Chrome Driver
		System.setProperty("webdriver.chrome.driver", "c:\\chromedriver.exe");
		driver = new ChromeDriver();

		// Navigate to particular Website
		driver.get("https://makeinjava.com");

		// Create instance of WebDriverWait class, We are giving wait time of 20 seconds
		WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));

		// Web driver will wait until element became visible
		// Below method will return element if element became visible 
		// If element not found, it will throw an exception
		WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("Enter Xpath here")));

		if (element.isDisplayed()) {
			System.out.println("Element is visible=");
		} else {
			System.out.println("Element is not visible");
		}

	}
}

Expected Conditions that can be used in Explicit Wait:

Exit mobile version