Selenium-Implicit and Explicit - Notes By ShariqSP

Try to click on Shopperstack login button using script

After executing the script, the login button will not be clicked due to the time taken to load the graphics. To handle this, you can use Thread.sleep, but it may not be efficient. Instead, use implicit or explicit waits to wait for the element to be clickable.

Implicit Wait

After the graphics are loaded, the login button is clicked. This works with both findElement and findElements.

Polling frequency: WebDriver will click the element if the element is present. If the element is not present or located, WebDriver will keep searching every 0.5 seconds. If the specified time limit is exceeded, WebDriver returns "no such element" or an empty list.


// Implicit wait
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.findElement(By.id("loginBtn")).click();
Thread.sleep(5000);
driver.close();
            

            // Implicit wait
            driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
            driver.findElement(By.id("loginBtn")).click();
            Thread.sleep(5000);
            driver.close();
                        

Explicit Wait

We can use conditions and we don't call the over time on the method. Before we specify the condition, we have to create an object of WebDriverWait. After the object is created, we can use explicitlyWait() using the until method. In the until method, we can use expectedCondition along with the methods. Here, the polling frequency is the same as in implicit.


            // Explicit wait
            WebDriverWait wait = new WebDriverWait(driver, 15);
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("loginBtn")));
            driver.findElement(By.id("loginBtn")).click();
                        

Difference between Implicit and Explicit Wait Commands in Selenium

Implicit Wait in Selenium Explicit Wait in Selenium
Applies to all elements in a test script. Applies only to specific elements as intended by the user.
No need to specify “ExpectedConditions” on the element to be located Must always specify “ExpectedConditions” on the element to be located
Most effective when used in a test case in which the elements are located within the time frame specified in implicit wait Most effective when used when the elements are taking a long time to load. Also useful for verifying the property of the element, such as visibilityOfElementLocated, elementToBeClickable, elementToBeSelected

Wait Interview Questions

  1. What is the purpose of waits in Selenium WebDriver?
  2. What is an Implicit Wait in Selenium WebDriver?
  3. What is an Explicit Wait in Selenium WebDriver?
  4. Explain the difference between Implicit and Explicit Waits.
  5. How do you set an Implicit Wait in Selenium WebDriver?
  6. How do you set an Explicit Wait in Selenium WebDriver?
  7. What is the default polling interval for Explicit Waits in Selenium WebDriver?
  8. Explain Expected Conditions in Explicit Waits.
  9. How do you use Expected Conditions with Explicit Waits?
  10. What is the purpose of WebDriverWait in Selenium WebDriver?
  11. What exceptions can occur when using waits in Selenium WebDriver?
  12. How do you handle timeouts when using waits in Selenium WebDriver?
  13. What are some common Expected Conditions used in Explicit Waits?
  14. How do you ignore specific exceptions during an Explicit Wait in Selenium WebDriver?
  15. What happens if an element is not found within the specified time in Explicit Waits?
  16. Explain the FluentWait class in Selenium WebDriver.
  17. How do you implement a custom Expected Condition in Selenium WebDriver?
  18. What is the difference between Thread.sleep() and Implicit/Explicit Waits?
  19. How do you handle stale element reference exceptions in Selenium WebDriver?
  20. What are the advantages of using Explicit Waits over Implicit Waits?

Multiple Choice Questions (MCQs)

  1. Which type of wait is set globally for the entire WebDriver instance?
    1. Implicit Wait
    2. Explicit Wait
    3. Fluent Wait
    4. Stale Wait
  2. Which type of wait waits for a certain condition to occur before proceeding with the execution?
    1. Implicit Wait
    2. Explicit Wait
    3. Fluent Wait
    4. Stale Wait
  3. Which class is used to implement Explicit Waits in Selenium WebDriver?
    1. WebDriverWait
    2. ImplicitWait
    3. FluentWait
    4. ExpectedConditions
  4. Which method is used to set an Implicit Wait in Selenium WebDriver?
    1. setImplicitWait()
    2. setImplicit()
    3. implicitlyWait()
    4. setWait()
  5. Which method is used to set an Explicit Wait in Selenium WebDriver?
    1. setExplicitWait()
    2. setWait()
    3. explicitlyWait()
    4. implicitlyWait()
  6. What is the default polling interval for Explicit Waits in Selenium WebDriver?
    1. 500 milliseconds
    2. 1 second
    3. 2 seconds
    4. 5 seconds
  7. Which class is used to define Expected Conditions in Selenium WebDriver?
    1. ExpectedConditions
    2. WebDriverWait
    3. FluentWait
    4. WebElementWait
  8. How do you handle timeouts when using waits in Selenium WebDriver?
    1. Using try-catch blocks
    2. Using sleep methods
    3. Using WebDriverWait
    4. Using Implicit Waits
  9. What happens if an element is not found within the specified time in Explicit Waits?
    1. WebDriver throws a NoSuchElementException
    2. WebDriver throws a TimeoutException
    3. WebDriver waits indefinitely
    4. WebDriver ignores the exception and proceeds with the execution
  10. Which method is used to ignore specific exceptions during an Explicit Wait in Selenium WebDriver?
    1. ignoreExceptions()
    2. except()
    3. ignore()
    4. exceptFor()