Explict Wait in Selenium

 I. Explicit Wait in selenium



ExplicitWait does not have any effect on findElement and findElements. ExplicitWait also called WebdriverWait.

WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully.

Syntax : WebDriverWait wait=new WebDriverWait( driver, timeoutInSeconds);

II. alertIsPresent()

Ex:

public class A
{
	public static void main(String[] args) throws InterruptedException
	{
		//open firefox
		WebDriver driver=new FirefoxDriver();
		//open google.com
		driver.get("https://abc.com");
		driver.findElement(BY.xpath("//button[@class='alert']")).click();
		WebDriverWait wait = new WebDriverWait(driver, 30 /*timeout in seconds*/);
		//throws TimeoutException if no alert is present
		wait.until(ExpectedConditions.alertIsPresent());
		driver.switchTo().alert().dismiss();
	}
}


III. elementToBeClickable()

Ex:

public class A
{
  public static void main(String[] args) throws InterruptedException
  {
      //open firefox
      WebDriver driver=new FirefoxDriver();
      //open google.com
      driver.get("https:abc.com");
      driver.findElement(By.xpath("//button[@id='btn1']")).click();

  }
}


IV. elementToBeSelected()

Ex:

public class A
{
  public static void main(String[] args) throws InterruptedException
  {
      //open firefox
      WebDriver driver=new FirefoxDriver();
      //open google.com
      driver.get("https//:abc.com");
driver.findElement(BY.xpath("//button[@class='alert']")).click(); WebDriverWait wait = new WebDriverWait(driver, 30 /*timeout in seconds*/); //throws TimeoutException if element is not selected in given time wait.until(ExpectedConditions.elementToBeSelected(By.xpath("//input[@id='hidden]")))); } }


V. textToBePresentInElement()

Ex:

public class A
{
  public static void main(String[] args) throws InterruptedException
  {
      //open firefox
      WebDriver driver=new FirefoxDriver();
      //open google.com
      driver.get("https//:abc.com");
driver.findElement(BY.xpath("//button[@class='alert']")).click(); WebDriverWait wait = new WebDriverWait(driver, 30 /*timeout in seconds*/); //throws TimeoutException if some text is not present in he webpage wait.until(ExpectedConditions.textToBePresentInElement(By.xpath("//input[@id='h2]")))); } }