Verify dynamic loading of element using Fluent Wait
Preconditions (2)
✅ Expected Result: The hidden element becomes visible after clicking the button and the test verifies its visibility successfully using Fluent Wait
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.
Now add data-driven testing with 3 different buttons that load different hidden elements using Fluent Wait
FluentWait in Selenium Python?from selenium.webdriver.support.ui import FluentWait
from selenium.common.exceptions import NoSuchElementException
wait = FluentWait(driver).with_timeout(5).polling_every(1).ignoring(NoSuchElementException)
element = wait.until(lambda d: d.find_element('id', 'submit'))
print(element.tag_name)wait = FluentWait(driver).with_timeout(10).polling_every(2)
element = wait.until(lambda d: d.find_element_by_id('login'))ElementNotInteractableException. Which FluentWait code snippet correctly implements this?Pass the test and this pattern moves unknown