Explicit wait in Selenium 3.0

Explicit wait in Selenium 3.0

All the elements on a web page may not get loaded at the same time when the page loads. We can’t access these elements unless they are visible (loaded), so we use wait to allow the elements to load. This is achieved through implicit and explicit wait in Selenium.

What happens if we don’t use wait?

We may get ElementNotVisibleException in case the web element is still not visible to the web driver and the program execution stops.

Selenium Web driver allows three types of wait:

  • Implicit Wait
  • Fluent Wait
  • Explicit Wait

In this tutorial, we are going to discuss explicit wait. We will discuss implicit wait in Selenium 3.0 in the next post.

This tutorial is part of Free Selenium 3.0 Tutorials by Techcanvass. These tutorials are categorized into Java, Selenium, TestNG, Automation Framework and Grid.

Explicit Wait in Selenium

What is Explicit Wait?

Explicit wait is a type of wait (applied through code piece), which is used to stop the execution till a specific condition is true.

We use WebDriverWait and ExpectedCondition classes of Selenium web driver to implement explicit wait.  Let’s take a simple example to understand the implementation:

WebElement elementID = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id(“elementID”)));

The following piece of code , in the code above,  specifies the condition of wait.

What’s the condition here? The condition is to wait till the specific element is present (or visible to the web driver).

until(ExpectedConditions.presenceOfElementLocated(By.id(“elementID”)));

The first part of the code specifies the wait time:

new WebDriverWait(driver, 10))

The web driver will wait for 10 seconds for the specified condition else it will throw an exception TimeoutException.

WebDriverWait checks the expected condition every 500 milliseconds till it is TRUE or the time limit elapses.

Some example of typical wait conditions are shown in the table below:

ConditionSelenium Web driver MethodExample
1An element is visible and enabledelementToBeClickable(By locator)WebElement list1 = (new WebDriverWait(driver, 10)).until(ExpectedConditions. elementToBeClickable (By.id(“list1”)));
2An element is selectedtitleContains(String Title)WebElement list1 = (new WebDriverWait(driver, 10)).until(ExpectedConditions. titleContains (“New Selenium Example”));
3Visibility of an elementvisibilityofElementLocated(By locator)WebElement list1 = (new WebDriverWait(driver, 10)).until(ExpectedConditions. visibilityofElementLocated (By.id(“list1”)));
4An alert is present.alertIsPresent()

 

WebElement list1 = (new WebDriverWait(driver, 10)).until(ExpectedConditions. alertIsPresent()));

To know the complete list of wait conditions, Type ExpectedConditions in eclipse IDE and press “cltr+space” bar.

Explicit Wait Example

We will take up an example to showcase explicit wait implementation. In this example, we have taken up Facebook login page. We have used explicit wait for the following conditions:

  • Checking the title of the page as “facebook”
  • Checking when the “Email” field becomes clickable

If the Email field is visible and clickable, we type in “Test” in that field. You can see the message in the eclipse console:

Email Text box is editable

Complete code is as shown below:

package basicSeleniumScripts;
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.WebDriverWait;

public class ExplicitWaitEx {

    public static void main(String[] args) {
        System.setProperty(“webdriver.chrome.driver”, “E:\\Selenium 3\\lib\\chromedriver.exe”);
        WebDriver driver;
        driver = new ChromeDriver();
        try
        {
      
        driver.manage().window().maximize();
        driver.get(“https://www.facebook.com/”);
      
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.titleContains(“Facebook”));
        wait.until(ExpectedConditions.elementToBeClickable(By.id(“email”)));
        System.out.println(“Email Text box is editable”);
        driver.findElement(By.id(“email”)).sendKeys(“Test”);
      
        driver.close();
           driver.quit();
         
    }
  
  
    catch (Exception e)
    {
        System.out.println(“The email text box is not yet visible”);
        //Quitting the web driver
        driver.close();
        driver.quit();
    }
  
  
    }}

About Techcanvass

Techcanvass offers IT certification courses for professionals. We are an IIBA endorsed education provider (EEP), iSQI ATP (for Certified Agile Business Analyst Training) as well as Agile Testing alliance partner for CP-SAT certification training in Selenium.

Our Selenium 3.0 Training programs along with DevOps courses are designed for Manual testing professionals with basics of Java and Python covered.

Selenium 3.0 Training with Java

Selenium Training Course

Selenium 3.0 Training with Python

python selenium training

Selenium Certification Training (CP-SAT)

Selenium Certification Course

DevOps Certification Training

DevOps Certification Training

Leave a Reply

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