Bird
Raised Fist0
Selenium Javatesting~15 mins

Implicit wait in Selenium Java - Build an Automation Script

Choose your learning style10 modes available
Verify implicit wait functionality on dynamic element loading
Preconditions (2)
Step 1: Open the browser and navigate to the test page URL
Step 2: Set implicit wait timeout to 10 seconds
Step 3: Click the button that triggers loading of a dynamic element
Step 4: Try to find the dynamic element by its id 'dynamicElement'
Step 5: Verify that the dynamic element is displayed on the page
✅ Expected Result: The dynamic element is found and displayed within the implicit wait timeout without throwing NoSuchElementException
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Assert that the dynamic element is displayed after clicking the button
Best Practices:
Use implicit wait only once after driver initialization
Use By.id or other stable locators for element identification
Avoid mixing implicit and explicit waits to prevent unexpected wait times
Automated Solution
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.time.Duration;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ImplicitWaitTest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        try {
            // Set implicit wait of 10 seconds
            driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

            // Navigate to test page
            driver.get("https://example.com/dynamic_loading");

            // Click button to load dynamic element
            WebElement loadButton = driver.findElement(By.id("loadButton"));
            loadButton.click();

            // Find dynamic element by id
            WebElement dynamicElement = driver.findElement(By.id("dynamicElement"));

            // Assert dynamic element is displayed
            assertTrue(dynamicElement.isDisplayed(), "Dynamic element should be visible after loading");

            System.out.println("Test Passed: Dynamic element is displayed.");
        } finally {
            driver.quit();
        }
    }
}

This code sets an implicit wait of 10 seconds right after creating the WebDriver. This means Selenium will wait up to 10 seconds when trying to find elements before throwing an error.

We open the test page, click the button that triggers loading the dynamic element, then try to find that element by its id. Because of the implicit wait, Selenium will keep trying until the element appears or the timeout expires.

We then assert that the element is displayed to confirm it loaded successfully. Finally, the browser closes.

This approach is simple and uses implicit wait correctly by setting it once. It uses stable locators and proper assertions to verify the test outcome.

Common Mistakes - 3 Pitfalls
Setting implicit wait multiple times in the test
Mixing implicit wait with explicit waits
Using unstable locators like absolute XPath
Bonus Challenge

Now add data-driven testing with 3 different buttons that load different dynamic elements

Show Hint

Practice

(1/5)
1. What is the main purpose of using implicit wait in Selenium WebDriver?
easy
A. To set a global wait time for all element searches
B. To wait for a specific element only once
C. To pause the test execution for a fixed time always
D. To speed up the test execution by skipping waits

Solution

  1. Step 1: Understand implicit wait behavior

    Implicit wait tells WebDriver to wait a set time when searching for elements before throwing an error.
  2. Step 2: Identify the scope of implicit wait

    It applies globally to all element searches, not just one element or fixed pauses.
  3. Final Answer:

    To set a global wait time for all element searches -> Option A
  4. Quick Check:

    Implicit wait = global element search wait [OK]
Hint: Implicit wait applies to all element searches globally [OK]
Common Mistakes:
  • Thinking implicit wait pauses test always
  • Confusing implicit wait with explicit wait
  • Assuming it waits for only one element
2. Which of the following is the correct syntax to set an implicit wait of 10 seconds in Selenium Java?
easy
A. driver.manage().timeouts().implicitlyWait(10);
B. driver.setImplicitWait(10);
C. driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
D. driver.wait(10, TimeUnit.SECONDS);

Solution

  1. Step 1: Recall correct method signature

    Since Selenium 4, implicit wait requires a Duration object, e.g., Duration.ofSeconds(10).
  2. Step 2: Check each option's correctness

    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); uses the correct method with Duration. driver.manage().timeouts().implicitlyWait(10); misses Duration, C and D are invalid methods.
  3. Final Answer:

    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); -> Option C
  4. Quick Check:

    Implicit wait needs Duration parameter [OK]
Hint: Use Duration.ofSeconds() for implicit wait time [OK]
Common Mistakes:
  • Passing int directly instead of Duration
  • Using non-existent methods like setImplicitWait
  • Confusing implicit wait with explicit wait syntax
3. What will happen if you set an implicit wait of 5 seconds and try to find an element that appears after 3 seconds?
medium
A. The driver will wait up to 5 seconds and find the element after 3 seconds
B. The element will be found immediately without waiting
C. The driver will throw NoSuchElementException immediately
D. The driver will wait exactly 5 seconds before finding the element

Solution

  1. Step 1: Understand implicit wait timing

    Implicit wait waits up to the specified time for an element to appear before failing.
  2. Step 2: Apply to the scenario

    The element appears after 3 seconds, which is less than 5 seconds, so the driver finds it after waiting 3 seconds.
  3. Final Answer:

    The driver will wait up to 5 seconds and find the element after 3 seconds -> Option A
  4. Quick Check:

    Implicit wait waits max time, stops early if found [OK]
Hint: Implicit wait stops waiting once element appears [OK]
Common Mistakes:
  • Assuming immediate find without wait
  • Thinking it always waits full time
  • Expecting immediate exception if element delayed
4. You wrote this code snippet to set implicit wait:
driver.manage().timeouts().implicitlyWait(10);
But your test throws a compilation error. What is the likely cause?
medium
A. You must import java.util.concurrent.TimeUnit for implicit wait
B. Implicit wait requires a Duration parameter, not an int
C. The method implicitlyWait() does not exist in WebDriver
D. Implicit wait cannot be set after driver initialization

Solution

  1. Step 1: Check method signature in Selenium 4+

    Implicit wait expects a Duration object, not a plain int.
  2. Step 2: Identify cause of compilation error

    Passing int causes compilation failure because no matching method exists.
  3. Final Answer:

    Implicit wait requires a Duration parameter, not an int -> Option B
  4. Quick Check:

    Implicit wait needs Duration, not int [OK]
Hint: Use Duration.ofSeconds() to avoid compilation errors [OK]
Common Mistakes:
  • Passing int instead of Duration
  • Missing import for Duration class
  • Assuming TimeUnit import fixes implicit wait
5. You set an implicit wait of 8 seconds in your test. Later, you add an explicit wait of 5 seconds for a specific element. What is the effective wait time when locating that element?
hard
A. 0 seconds, because waits cancel each other out
B. 8 seconds, because implicit wait always overrides explicit wait
C. 13 seconds, sum of implicit and explicit waits
D. 5 seconds, because explicit wait overrides implicit wait for that element

Solution

  1. Step 1: Understand interaction of implicit and explicit waits

    Explicit wait applies to specific elements and overrides implicit wait for that action.
  2. Step 2: Determine effective wait time

    Explicit wait of 5 seconds is used for that element, ignoring the 8-second implicit wait.
  3. Final Answer:

    5 seconds, because explicit wait overrides implicit wait for that element -> Option D
  4. Quick Check:

    Explicit wait overrides implicit wait [OK]
Hint: Explicit wait overrides implicit wait for targeted elements [OK]
Common Mistakes:
  • Adding implicit and explicit wait times together
  • Assuming implicit wait always dominates
  • Thinking waits cancel each other
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard