Wait for the Download, Not the Clock
intermediate Python 20 minAn export test sleeps five seconds and then checks the file exists. It fails on slow CI and is useless on Grid. Wait for the finished file properly.
The Scenario
The reports page has an “Export CSV” button that generates a file server-side and streams it to the browser. Locally the test passes. In CI it fails about one run in ten, whenever report generation takes more than five seconds. When the team moved the suite to Selenium Grid, it failed every time: the file was downloaded inside the Node container, and the test looked for it on the CI runner.
There is a second, quieter bug: every test downloads into the same ~/Downloads folder, so a report.csv left over from yesterday makes the test pass even when today’s export is broken.
Your Task
- For local runs, give each test its own download directory and wait until the file exists and is complete (no
.crdownload/.parttemp file, non-zero size). - For Grid runs, enable managed downloads and use the downloads API to wait for and fetch the file.
- Assert something about the file’s content, not just its existence.
What Good Looks Like
- No
time.sleep. - An isolated download directory per test, so stale files cannot produce false passes.
- A custom wait condition with a timeout message that names the missing file.
- Grid runs use
enable_downloadswithget_downloadable_files()anddownload_file(). The oldGET /session/{id}/se/files/{fileName}endpoint was removed from the Java server in 4.49, so don’t build on it from any binding.
Your task
An export test sleeps five seconds and then checks the file exists. It fails on slow CI and is useless on Grid. Wait for the finished file properly.
import os, time
def test_export_csv(driver):
driver.get("https://app.example.com/reports")
driver.find_element(By.ID, "export-csv").click()
time.sleep(5)
path = os.path.expanduser("~/Downloads/report.csv")
assert os.path.exists(path)
Need Help?
Solution
Try to solve the challenge yourself before revealing the solution.
Lessons that cover this
- Grid on Docker and Kubernetes Deploy Selenium Grid 4.4x for real workloads: Dynamic Grid with Docker, the native Kubernetes session factory, the Helm chart with autoscaling, Redis-backed state, session events, video, and observability.
- Custom Wait Conditions Write your own expected conditions with lambdas and classes, wait for attribute and text changes, element counts, JavaScript state, network idleness via BiDi, and compose conditions cleanly.
- Browser Options & Configuration Learn how to configure browser options in Selenium WebDriver, including headless mode, window size, and custom arguments.