Make Selenium wait 10 seconds.

SeleniumAutomation TestingTesting Tools

We can make Selenium wait for 10 seconds. This can be done by using the Thread.sleep method. Here, the wait time (10 seconds) is passed as a parameter to the method.

We can also use the synchronization concept in Selenium for waiting. There are two kinds of wait − implicit and explicit. Both these are of dynamic nature, however the implicit wait is applied to every step of automation, the explicit wait is applicable only to a particular element.

Example

Code Implementation with sleep method.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class WaitThrd{
   public static void main(String[] args)
   throws InterruptedException{
      System.setProperty("webdriver.chrome.driver",
         "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.get("https://www.tutorialspoint.com/index.htm");
      // wait time added
      Thread.sleep(200);
      // identify element,
      WebElement m=driver.findElement(By.id("gsc−i−id1"));
      m.sendKeys("Java");
      driver.close();
   }
}

Example

Code Implementation with implicit wait.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class WaitImplicit{
   public static void main(String[] args)
   throws InterruptedException{
      System.setProperty("webdriver.chrome.driver",
         "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      // implicit wait
      driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
      driver.get("https://www.tutorialspoint.com/index.htm");
      // identify element,
      WebElement m=driver.findElement(By.id("gsc−i−id1"));
      m.sendKeys("Python");
      driver.close();
   }
}

Example

Code Implementation with explicit wait.

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 WaitExplicit{
   public static void main(String[] args)
   throws InterruptedException{
      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,
      WebElement l=driver.findElement(By.xpath("//*[text()='Library']"));
      l.click();
      //explicit wait
      WebDriverWait w = new WebDriverWait(driver,7);
      //expected condition
      w.until(ExpectedConditions.
      invisibilityOfElementLocated(By.xpath("//*[@class='mui−btn']")));
      driver.close();
   }
}
raja
Published on 30-Jan-2021 17:38:50
Advertisements