Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests

Implicit wait in Selenium is a feature that tells the WebDriver to poll the DOM for a certain amount of time when trying to find an element if it is not immediately available. This wait is applied globally to all elements in the test script, meaning any element search will wait up to the specified time before throwing a NoSuchElementException.

How Implicit Wait Works

When an implicit wait is set, the WebDriver will wait for the specified time duration before throwing a NoSuchElementException. During this time, the WebDriver will periodically check the DOM to see if the element has appeared.

For example, if an implicit wait of 10 seconds is set, WebDriver will try to find the element, and if the element is not found, it will keep trying for up to 10 seconds before throwing an exception. If the element appears at any point during these 10 seconds, the WebDriver will proceed with the next step in the script.

Setting Implicit Wait

To set an implicit wait in Selenium using Java, you can use the ‘manage().timeouts().implicitlyWait()‘ method. Here’s how you do it

				
					import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class ImplicitWaitExample {
    public static void main(String[] args) {
        // Set the path of the WebDriver executable
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        // Initialize the WebDriver (Chrome in this case)
        WebDriver driver = new ChromeDriver();

        // Set implicit wait of 10 seconds
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        // Navigate to a webpage
        driver.get("https://example.com");

        // Try to find an element
        WebElement element = driver.findElement(By.id("someElementId"));

        // Interact with the element
        element.click();

        // Close the browser
        driver.quit();
    }
}

				
			

Explanation of the Code

  1. Setting the WebDriver Path: The path to the ‘chromedriver‘ executable is set using ‘System.setProperty‘.

  2. Initializing the WebDriver: A new instance of ‘ChromeDriver‘ is created.

  3. Setting Implicit Wait: The implicit wait is set to 10 seconds using ‘driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);‘.

  4. Navigating to a Webpage: The ‘driver.get("https://example.com");‘ method is used to navigate to a webpage.

  5. Finding and Interacting with an Element: The ‘findElement‘ method is used to locate an element by its ID. If the element is not immediately found, WebDriver will wait up to 10 seconds before throwing a ‘NoSuchElementException‘.

  6. Closing the Browser: The ‘driver.quit();‘ method is used to close the browser.

Important Points to Note

  1. Global Setting: The implicit wait is a global setting and applies to all elements in the test script.

  2. Affects All ‘findElement‘ Calls: Once set, the implicit wait will affect all subsequent ‘findElement‘ and ‘findElements‘ calls.

  3. Duration: The implicit wait duration is the maximum time WebDriver will wait. If the element is found before the specified time, WebDriver will proceed without waiting for the entire duration.

  4. Overhead: While implicit wait helps handle dynamic content, it may add unnecessary overhead if not used judiciously, as it will wait for the maximum time specified even for elements that are available immediately

Comparison with Explicit Wait

While implicit wait is useful for setting a default wait time, it might not always be the best choice for all scenarios. For more control over waiting conditions, explicit wait (using ‘WebDriverWait‘) can be used to wait for specific conditions to be met before proceeding.

				
					import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import java.time.Duration;

public class ExplicitWaitExample {
    public static void main(String[] args) {
        // Set the path of the WebDriver executable
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        // Initialize the WebDriver (Chrome in this case)
        WebDriver driver = new ChromeDriver();

        // Navigate to a webpage
        driver.get("https://example.com");

        // Set explicit wait
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

        // Wait for the element to be clickable
        WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someElementId")));

        // Interact with the element
        element.click();

        // Close the browser
        driver.quit();
    }
}

				
			

In this example, the ‘WebDriverWait‘ and ‘ExpectedConditions‘ classes are used to wait until a specific condition (element to be clickable) is met.

By understanding and using implicit wait correctly, you can make your Selenium scripts more robust and less prone to failure due to timing issues.

Scroll to Top