Implicit and Explicit wait in Selenium

During automation script development, its common that at least once in their test execution, their scripts have failed due to WebDriver couldn’t able to find element in the web page. This issue can be resolved by using selenium wait conditions appropriately. Page load speed depends on the how fast the response is coming from web server or strength of internet connections. When the page is loading slowly, few elements may not load in time hence selenium will not be able to identify the elements. Wait in selenium is of two types:

  • Implicit wait
  • Explicit wait

Implicit wait:

It is applicable for all the web elements in the page. Using this condition, we are telling the driver to wait for specified period of time for each web element, until it finds it out.

Syntax:

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

In the above code, we are setting default wait time as 30 seconds. Web driver will wait for 30 seconds for each web element. If it doesn’t find out the element within this period, it will throw NoSuchElementException.

Explicit wait:

When a particular web element takes more time to appear on the page, its better to use explicit wait condition. Implicit wait will not be applicable for the element on which we are using explicit wait condition. In the below code, web driver waits for 30 seconds for web element.

Syntax:

WebElement elem1 ;
elem1 = (new WebDriverWait(driver, 30)).until(ExpectedConditions.elementToBeClickable(By.linkText(“TestNG”)));

WebElement elem2 ;
elem2 = (new WebDriverWait(driver, 30)).until(ExpectedConditions.visibilityOfElementLocated(By.linkText(“Hands on”)));

Example:

package com;

import java.util.concurrent.TimeUnit;

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.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class WaitsInSelenium {

static WebDriver driver;

@BeforeTest
public void before() throws Exception {
// chrome standalone server
System.setProperty(“webdriver.chrome.driver”,
“C:\\Users\\username\\Downloads\\chromedriver_win32\\chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();

// implicit wait. Applicable for all web elements
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

// this opens the url
driver.get(“https://selenium99.com/selenium-tutorials/”);
Thread.sleep(1000);
}

@Test
public void waits_selenium() throws Exception {

WebElement elem1 ;
elem1 = (new WebDriverWait(driver, 30)).until(ExpectedConditions.elementToBeClickable(By.linkText(“TestNG”)));

WebElement elem2 ;
elem2 = (new WebDriverWait(driver, 30)).until(ExpectedConditions.visibilityOfElementLocated(By.linkText(“Hands on”)));

System.out.println(“Test passed.”);
}

@AfterTest
public void tearDown() {

driver.close();
}
}

Output on the console:

Test passed.
PASSED: navigate_selenium


Other selenium topics:

TestNG

How to handle mouse hover events using Selenium WebDriver

WebElement actions in selenium WebDriver

How to take screenshot using Selenium WebDriver

Navigate commands in Selenium WebDriver


Tags: waits in selenium, implicit wait and explicit wait, selenium webdriver waits