Getting Selenium to pause for X seconds.


We can get Selenium to pause for X seconds with the concept of synchronization. There are two types of waits − implicit and explicit. Apart from this there is the Thread.sleep method that halts Selenium for a certain time. The wait time is passed as an argument to the method.

Example

Code Implementation with Thread.sleep.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class ThreadWt{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
      "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.get("https://www.tutorialspoint.com/index.htm");
      // identify element, enter text
      WebElement n=driver.findElement(By.className("gsc-input"));
      // wait of 200 ms applied
      Thread.sleep(200);
      n.sendKeys("Selenium");
      driver.quit();
   }
}

We can specify an implicit wait. It shall keep the driver to wait for a specific amount of time for an element to be available.

Syntax

driver.manage().timeouts().implicitlyWait();

The implicit is a global wait applied to every element on the page and is dynamic in nature.

Example

Code Implementation with implicit wait.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class ImplicitWt{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
      "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "https://www.tutorialspoint.com/index.htm";
      driver.get(url);
      // wait of 5 seconds
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      // identify element, enter text
      WebElement n=driver.findElement(By.className("gsc−input"));
      n.sendKeys("Selenium");
      driver.quit();
   }
}

The explicit wait is also used and it is applied to a specific element on the page. It is a WebDriverWait that works in association with the Expected Condition class. It is also dynamic in nature.

Expected conditions for explicit waits are −

  • titleContains

  • alertIsPresent

  • invisibilityOfElementLocated

  • invisibilityOfElementWithText

  • textToBePresentInElement

  • visibilityOfElementLocated

  • presenceOfAllElementsLocatedBy

  • visibilityOf

  • presenceOfElementLocated

  • elementToBeClickable

  • stalenessOf

Example

Code Implementation with explicit wait.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ExpltWaits{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
      "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.get("https://www.tutorialspoint.com/index.htm");
      // identify element and click()
      driver.findElement(By.xpath("//*[text()='Library']")).click();
      // expected condition - invisibility condition
      WebDriverWait wt = new WebDriverWait(driver,5);
      // invisibilityOfElementLocated condition
      wt.until(ExpectedConditions.
      invisibilityOfElementLocated(By.xpath("//*[@class='mui-btn']")));
      driver.close();
   }
}

Updated on: 30-Nov-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements