0
0
Selenium Pythontesting~15 mins

Fluent waits in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify dynamic loading of element using Fluent Wait
Preconditions (2)
Step 1: Open the test page URL in a browser
Step 2: Click the button that triggers loading of the hidden element
Step 3: Use Fluent Wait to wait for the hidden element to be visible
Step 4: Verify that the hidden element is displayed on the page
✅ Expected Result: The hidden element becomes visible after clicking the button and the test verifies its visibility successfully using Fluent Wait
Automation Requirements - Selenium with Python
Assertions Needed:
Assert the hidden element is displayed after waiting
Assert no timeout exception occurs during wait
Best Practices:
Use Fluent Wait with polling interval and timeout
Handle exceptions like NoSuchElementException during wait
Use By locators for element identification
Avoid hardcoded sleeps; rely on waits
Automated Solution
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.wait import FluentWait
import time

# Setup WebDriver (Chrome example)
driver = webdriver.Chrome()
try:
    driver.get('https://example.com/dynamic_loading')  # Replace with actual test URL

    # Click the button to start loading hidden element
    start_button = driver.find_element(By.ID, 'startButton')
    start_button.click()

    # Setup Fluent Wait: timeout 15 seconds, polling every 1 second
    wait = WebDriverWait(driver, 15, poll_frequency=1, ignored_exceptions=[NoSuchElementException])

    # Wait until the hidden element is visible
    hidden_element = wait.until(EC.visibility_of_element_located((By.ID, 'hiddenElement')))

    # Assert the element is displayed
    assert hidden_element.is_displayed(), 'Hidden element should be visible after wait'

finally:
    driver.quit()

This script opens the test page and clicks the button that triggers loading of a hidden element.

It uses Selenium's WebDriverWait with a polling interval of 1 second and a timeout of 15 seconds to implement a Fluent Wait.

The wait ignores NoSuchElementException during polling to avoid failing early.

Once the hidden element is visible, the script asserts it is displayed.

Finally, the browser is closed to clean up.

Common Mistakes - 4 Pitfalls
Using time.sleep() instead of Fluent Wait
Not ignoring exceptions during wait
Using hardcoded XPath locators
Not quitting the driver after test
Bonus Challenge

Now add data-driven testing with 3 different buttons that load different hidden elements using Fluent Wait

Show Hint