Waits

In Selenium, waiting is an essential concept to handle synchronization issues between the automation script and the web application being tested. The primary purpose of waiting is to ensure that the script does not attempt to interact with elements on the web page before they are properly loaded or become available. There are two main types of waiting in Selenium: Implicit Wait and Explicit Wait.

Implicit Wait



An implicit wait instructs the WebDriver to wait for a certain amount of time before throwing an exception. It is set globally for the entire script execution.

In the following example the implicit wait ensures that the WebDriver waits up to 30 seconds for an element to be present before throwing an exception. This wait is applied for every finding element operation throughout the script.

Java
C#
Python
Javascript
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("https://www.testautomationstudio.com/demo/wait/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
WebElement wait1Elem = driver.findElement(By.id("wait1"));
wait1Elem.click();
driver.quit();
Copy
IWebDriver driver = new ChromeDriver();
driver.Url = "https://www.testautomationstudio.com/demo/wait/";
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
IWebElement wait1Elem = driver.FindElement(By.Id("wait1"));
wait1Elem.Click();
driver.Quit();
Python code coming soon
Javascript code coming soon
Kotlin code coming soon

Explicit Wait



An explicit wait is more targeted and allows the script to wait for a specific condition to be met before proceeding. It is applied to a particular element or a certain condition. The explicit wait in the following example is applied only to the specific WebElement ie element with id Elem1StatusA. The script waits up to 10 seconds for the element to be visible before throwing an exception.

Java
C#
Python
Javascript
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("https://www.testautomationstudio.com/demo/wait/");
driver.manage().window().maximize();
driver.findElement(By.id("wait1")).click();
Wait<WebDriver> wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(d -> d.findElement(By.id("Elem1StatusA")).isDisplayed());
driver.quit();
Copy
IWebDriver driver = new ChromeDriver();
driver.Url = "https://www.testautomationstudio.com/demo/wait/";
driver.Manage().Window.Maximize();
driver.FindElement(By.Id("wait1")).Click();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(d =>d.FindElement(By.Id("wait1Status")).Text.Equals("Wait 1 Status"));
driver.Quit();
Python code coming soon
Javascript code coming soon
Kotlin code coming soon

FluentWait

Fluent Wait is an advanced type of Explicit Wait in Selenium that allows you to customize various parameters that will change how the conditions are evaluated. You can define custom polling intervals and exceptions to wait for a certain condition to be met. It is more flexible than the regular WebDriverWait by providing a mechanism for defining the maximum amount of time to wait for a condition and the frequency with which to check the condition.

Java
C#
Python
Javascript
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("https://www.testautomationstudio.com/demo/wait/");
driver.manage().window().maximize();

driver.findElement(By.id("wait1")).click();

FluentWait<WebDriver> wait = new FluentWait<>(driver);
wait.withTimeout(Duration.ofSeconds(10));
wait.pollingEvery(Duration.ofMillis(500));
wait.ignoring(NoSuchElementException.class);

wait.until(d-> d.findElement(By.id("Elem1StatusA")).isDisplayed());

driver.quit();
Copy
IWebDriver driver = new ChromeDriver();
driver.Url = "https://www.testautomationstudio.com/demo/wait/";
driver.Manage().Window.Maximize();
driver.FindElement(By.Id("wait1")).Click();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
wait.PollingInterval = TimeSpan.FromSeconds(2);
wait.IgnoreExceptionTypes(typeof(NoSuchElementException));
wait.Until(d => d.FindElement(By.Id("wait1Status")).Text.Equals("Wait 1 Status"));
driver.Quit();
Python code coming soon
Javascript code coming soon
Kotlin code coming soon

In the above example, the script waits up to 10 seconds for the element with the ID Elem1StatusA to be displayed. It checks for the element's presence every 500 milliseconds. The NoSuchElementException is ignored during the polling.