How to Use wait() Function in Python?

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.gov

I have executed the above example and added the screenshot below.

wait() function in Python

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:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.