Waits in Selenium || Implicit wait || Explicit Wait || Selenium Automation with Java

Опубликовано: 18 Март 2026
на канале: Knowledge Share
198
4

In this session you will learn what are waits in Selenium and types of available to Implement in Selenium

You can download this example program by scrolling to the bottom on the description and also you can import the complete example programs from below GitHub Repository
https://github.com/knowledgeshare-tec...
*******************************************************************
You can see step by step selenium sessions from below playlist
*******************************************************************
https://www.youtube.com/watch?v=JUDMG...

For regular updates you can join this Group
***********************************************
  / 47542.  .
***********************
About Waits
***********************
Why Waits in Selenium ?
Waits are used to handle pages which are slow in loading or when identifying elements loading in another window
Handling web applications developed with Ajax and Javascript, as different pages contains or loads in different time intervals
To Handle exceptions related to Elements

Note :
This are all Dynamic waits and when in the element is found with in the time specified, rest of the time is saved or ignored.

Types of Waits in Selenium ?
Implicit Waits - Applicable for entire Test Script/ all elements in the script
We can give specific time to wait
driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS);
Explicit Waits - Applicable for only one instance/Specific element until the a particular condition is met
We can Give a Condition and Specific time to wait
WebDriverWait wait = new WebDriverWait(WebDriverRefrence,TimeOut);
ExpectedConditions class is used in explicit wait
Fluent Waits – Applicable for specific element with polling frequency, ignore specific conditions
WaitLTWebDriverGT wait = new FluentWaitLTWebDriverGT(WebDriver reference)
.withTimeout(Duration.ofSeconds(SECONDS))
.pollingEvery(Duration.ofSeconds(SECONDS))
.ignoring(Exception.class);


***********************
Implicit Wait Example
***********************
package Waits_Examples;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class implicit_wait_Example {

public static void main(String[] args)
{
/*
System.setProperty("webdriver.chrome.driver",
".\\drivers\\chromedriver.exe"); WebDriver driver=new ChromeDriver();
*/

System.setProperty("webdriver.gecko.driver", ".\\drivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
//Implicit wait
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

driver.get("https://www.google.com/");
driver.findElement(By.name("q")).sendKeys("artificial intelligence");
Actions actions=new Actions(driver);
actions.sendKeys(Keys.ENTER).build().perform();

driver.findElement(By.xpath("(//*[@class='cfxYMc JfZTW c4Djg MUxGbd v0nnCb'])[2]")).click();

}

}


************************
Explicit Wait Example
*************************
package Waits_Examples;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class explicit_wait_Example {

public static void main(String[] args) {


System.setProperty("webdriver.gecko.driver", ".\\drivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();


/*
System.setProperty("webdriver.chrome.driver",".\\drivers\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
*/

// Explicit Wait

WebDriverWait wait = new WebDriverWait(driver, 20);

driver.get("https://www.google.com/");
driver.findElement(By.name("q")).sendKeys("artificial intelligence");
Actions actions = new Actions(driver);
actions.sendKeys(Keys.ENTER).build().perform();

// WebElement link_to_click=driver.findElement(By.xpath("(//*[@class='cfxYMc
// JfZTW c4Djg MUxGbd v0nnCb'])[2]"));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//*[@class='cfxYMc JfZTW c4Djg MUxGbd v0nnCb'])[2]")));
driver.findElement(By.xpath("(//*[@class='cfxYMc JfZTW c4Djg MUxGbd v0nnCb'])[2]")).click();

}

}