In this tutorial, I will explain how to use wait functions in Python. As a Python developer working on projects for clients in the USA, I frequently need to build in strategic delays and pauses. Effective use of Python wait functions allows you to control the flow and timing of your code. Let us see how the various wait methods in Python work, with examples.
The time.sleep() Function in Python
One of the most basic and commonly used wait functions in Python is time.sleep(). This function suspends execution of the current thread for a specified number of seconds.
Let’s say you’re building a website scraper that needs to pause between requests to avoid overloading the server. Here’s how you could use time.sleep() to add a 5-second wait:
import time
def scrape_website(url):
print(f"Scraping: {url}")
# Scraping code here
time.sleep(5) # Pause for 5 seconds before the next request
urls = [
"https://www.whitehouse.gov",
"https://www.nasa.gov",
"https://www.nps.gov"
]
for url in urls:
scrape_website(url)Output:
Scraping: https://www.whitehouse.gov
Scraping: https://www.nasa.gov
Scraping: https://www.nps.govI have executed the above example and added the screenshot below.

In this example, the scraper will process the list of URLs, which includes popular US government websites like whitehouse.gov and nasa.gov, pausing for 5 seconds between each one.
Read How to Use the Python pop() Function?
Implicit Waits with WebDriverWait in Python
When working with Selenium WebDriver for browser automation, implicit waits are a handy way to make your code wait for elements to appear before interacting with them. The WebDriverWait class in combination with expected_conditions allows you to wait for specific conditions to be met.
Suppose you’re automating filling out a form on a US government website that takes a few seconds to load. Here’s how you could wait up to 10 seconds for the form to be present:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.usa.gov/forms")
try:
form = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "form-id"))
)
# Interact with form here
finally:
driver.quit()The WebDriverWait will keep checking for up to 10 seconds until the form element is present on the page. This ensures your automation won’t fail just because of a slow loading page.
check out Is Python a High Level Language
Customize Wait Conditions in Python
In some cases, you may need to wait for custom conditions beyond what’s provided by Selenium’s expected_conditions. Python’s wait module gives you the tools to define your custom wait conditions.
For example, let’s say you need to wait for a status message to display “Ready” on an internal company web app dashboard:
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
# Initialize the browser
driver = webdriver.Chrome()
try:
# Navigate to the web app dashboard
driver.get("https://dashboard.examplecompany.com")
# Wait up to 30 seconds for the status message to display "Ready"
status_element = WebDriverWait(driver, 30).until(
EC.text_to_be_present_in_element((By.ID, "status-display"), "Ready")
)
if status_element:
print("Dashboard status: Ready!")
else:
print("Status not ready!")
except Exception as e:
print(f"Error occurred: {e}")
finally:
# Close the browser
driver.quit()By using asyncio.gather, the code waits for all the fetch tasks to complete before proceeding to process the returned data. This allows you to efficiently wait for multiple asynchronous operations.
Read How to Convert a List to an Array in Python
Conclusion
In this tutorial, I helped you learn how to use wait functions in Python. Whether you need a simple time.sleep(), more advanced Selenium waits, custom wait conditions, or asynchronous pauses with asyncio, there’s a waiting technique to suit your needs.
You may also like to read:
- How to Distinguish Between Arrays and Lists in Python
- Compare Lists, Tuples, Sets, and Dictionaries in Python
- Python Lists of Floats

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.