Reading list Switch to dark mode

    Implicit and Explicit Waits in Selenium

    Updated 13 January 2020

    What are Waits?

    Waits helps the user to find out the issues while navigating to different web pages. This can be done by refreshing the web pages to reload the web elements. It mights happens that page reload time can be more because of some ajax request due to which webelement takes more time to reflect.

    Need of Waits in Selenium

    Most of the time when we run our Selenium script we get ElementNotVisibleException exception. This happens because Most of the Web Application uses javascript and ajax. Our script execute before getting the response of ajax or javascript request because of which elements are not visible at the time of execution of Script.

    We can overcome this problem by using Selenium Waits. Selenium offers two types of Waits-

    1.Implicit Waits

    2.Explicit Waits

    Start your headless eCommerce
    now.
    Find out More

    Implicit Waits

    It will be applicable globally, that means it will work for all the web elements throughout the driver instance.

    Implicit wait will notify the driver to wait for the given amount of time before throwing an exception “No Such Element Exception “. By default it waits for 0 seconds. After setting the time it will wait for that particular time before throwing an exception.

    Syntax of Implicit wait:

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

    Lets take an example to see how Implicit wait works:

    package Test1;
    
    
     import java.util.concurrent.TimeUnit;
     import org.openqa.selenium.By;
     import org.openqa.selenium.WebDriver;
     import org.openqa.selenium.chrome.ChromeDriver;
     import org.openqa.selenium.support.ui.Select;
     import org.testng.annotations.AfterMethod;
     import org.testng.annotations.BeforeMethod;
     import org.testng.annotations.Test;
     public class Category {
     WebDriver driver;
    
     @BeforeMethod()
         public void setup() {
         System.setProperty("webdriver.chrome.driver", "/home/users/jyoti.singh/eclipse-work/selenium-java-3.141.59/chromedriver.exe");
         driver=new ChromeDriver();
         driver.manage().window().maximize();
         driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
         driver.navigate().to("http://192.168.15.201/bagistonew/public/admin/login");
         driver.findElement(By.id("email")).sendKeys("[email protected]");
         driver.findElement(By.id("password")).sendKeys("admin123");
         driver.findElement(By.className("btn-primary")).click(); 
         driver.findElement(By.xpath("//a//span[contains(text(),'Catalog')]")).click();
         driver.findElement(By.xpath("//a[contains(text(),'Categories')]")).click();
         driver.findElement(By.xpath("//a[@class='btn btn-lg btn-primary']")).click();
    
    }
    @Test()
     public void addwithoutdata() {
         driver.findElement(By.xpath("/html[1]/body[1]/div[1]/div[4]/div[1]/div[2]/div[2]/form[1]/div[1]/div[2]/button[1]")).click();
    }
    
    @AfterMethod()
         public void teardown() {
           driver.close();
    }
    }

    In the above code implicit wait of 30 seconds that means, it will wait for 30 seconds before throwing the exception. Implicit wait takes two parameter, first one is time as integer and second one is measurement of time in form of MINUTE, SECONDS, MILLISECONDS, MICROSECONDS, NANOSECONDS, DAYS, HOURS, etc.

    Explicit Wait

    We use Explicit wait when we have to state driver to wait till some certain condition matches (Expected condition) before throwing an error “ElementNotVisibleException“. It can be Implemented by WebDriverWait class. Unlike Implicit wait, Explicit waits only available for specified Elements.

    After declaring Explicit wait we have to define “Expected Condition”.

    Syntax for Explicit Wait:

    WebDriverWait wait = new WebDriverWait (WebDriverReference,TimeOut);

    When to Use Explicit wait

    Suppose you have to login in some website and after login it takes some time to load data and to reach home page. This page is dynamic so sometime it takes 15 seconds to load the homepage and sometime it takes 30 seconds. So in this case you can use Explicit wait to  wait until a specific page is not present.

    package Test1;
    
    import java.util.concurrent.TimeUnit;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.Select;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.Test;
    
    public class Category {
    WebDriver driver;
    
    @BeforeMethod()
    public void setup() {
    	System.setProperty("webdriver.chrome.driver", "/home/users/jyoti.singh/eclipse-work/selenium-java-3.141.59/chromedriver.exe");
    	driver=new ChromeDriver();
    	driver.manage().window().maximize();
    	driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    	driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    
    	driver.navigate().to("http://192.168.15.201/bagistonew/public/admin/login");
    	driver.findElement(By.id("email")).sendKeys("[email protected]");
        driver.findElement(By.id("password")).sendKeys("admin123");
        driver.findElement(By.className("btn-primary")).click(); 
        WebDriverWait wait = new WebDriverWait(driver,30);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a//span[contains(text(),'Catalog')]")));
        driver.findElement(By.xpath("//a//span[contains(text(),'Catalog')]")).click();
        driver.findElement(By.xpath("//a[contains(text(),'Categories')]")).click();
        driver.findElement(By.xpath("//a[@class='btn btn-lg btn-primary']")).click();
    }
    
    @Test()
    public void addwithoutdata() {
    	driver.findElement(By.xpath("/html[1]/body[1]/div[1]/div[4]/div[1]/div[2]/div[2]/form[1]/div[1]/div[2]/button[1]")).click();
    	}
    @AfterMethod
     public void tear() {
    	 driver.close();
     }
    }

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home