AI-Generated Selenium Tests Still Flaky? Check What They Wait For
The AI assistant fixed a flaky Selenium test by doubling a sleep, and it passed once. What does a reliable test wait for instead?

- A sleep waits for a fixed duration without checking whether the application is ready.
- Presence, visibility and enabled state answer different questions; completion needs its own check.
- Review the condition behind an AI-generated fix, along with mixed waits and shared browser sessions.
A delay cannot establish application readiness
Changing a five-second sleep to ten seconds may make a failing run pass. It still leaves the test dependent on timing. Slower runs can fail again; faster ones spend time waiting after the application is ready.
Selenium's waiting guide explains why navigation completing is insufficient: JavaScript can continue changing the page after the browser reaches its expected loading state. An explicit wait instead polls for a condition, proceeding when it becomes true or failing at the timeout.
In a September 29 post, Selenium's David Burns connected recurring agent mistakes to outdated Selenium examples. Fixed sleeps and fragile XPath locators are among the patterns he calls out.
Choose the state the next operation actually requires
The Python expected-conditions reference distinguishes several useful checks:
| Condition | What it establishes |
|---|---|
presence_of_element_located | The element exists in the DOM; it need not be visible. |
visibility_of_element_located | The element is displayed with nonzero dimensions. |
element_to_be_clickable | The element is visible and enabled. |
text_to_be_present_in_element | The element contains the expected text. |
Finding a button does not establish that it is ready to use. A successful readiness check also says nothing about whether the subsequent save has completed.
Give the result its own wait
This illustrative fragment uses an existing driver. Replace the IDs and completion text with those of the application. Ten seconds is an example limit.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
button = wait.until(
EC.element_to_be_clickable((By.ID, "save-button"))
)
button.click()
wait.until(
EC.text_to_be_present_in_element((By.ID, "save-status"), "Saved")
) If an old “Saved” message remains on screen, choose a completion condition that distinguishes this save from the previous one. Otherwise, the second wait can accept stale success text.
Also inspect the session's implicit wait. It applies to element lookup and returns once an element is found. Selenium explicitly advises against combining it with explicit waits because the resulting timing can become unpredictable.
Ask the agent to identify the missing condition
The official AI-agent guide recommends checking locators against the application, running the individual test and repeating it before trusting the result. A repair should explain which required condition was false, rather than merely increasing a timeout.
Keep the starting state separate from synchronization. Selenium recommends a fresh WebDriver for each test. Reusing another test's session introduces a dependency that a better wait does not remove.
The useful review question is concrete: what must become true before the next line runs, and what proves this operation finished?