Whenever you are using Selenium WebDriver in Python, you should wait until the page or certain elements are fully loaded before interacting with them. Without proper waits, your script will try to interact with elements that haven’t appeared yet and will fail.
Here are the ways to handle waits in Selenium:
Method 1: Use implicitly_wait()
Automatically waits for a certain amount of time when trying to find elements.
from selenium import webdriverdriver = webdriver.Chrome()
driver.implicitly_wait(10) # waits up to 10 seconds
driver.get(“https://example.com”)
element = driver.find_element(“id”, “myElement”) # waits until found
Applies a global timeout — Selenium will wait up to the specified time before throwing an exception.
Method 2: Use WebDriverWait with expected_conditions (Recommended)
Waits until a certain condition is true before continuing.
from selenium import webdriverfrom selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get(“https://example.com”)
# Wait until an element is visible
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, “myElement”))
)
print(element.text)
Good for waiting on dynamic elements like buttons, forms or AJAX-loaded content.
Method 3: Use driver.execute_script() to Wait for Full Page Load
Checks if the document is fully loaded using JavaScript.
from selenium import webdriverimport time
driver = webdriver.Chrome()
driver.get(“https://example.com”)
# Wait until the page is fully loaded
while driver.execute_script(“return document.readyState;”) != “complete”:
time.sleep(0.5)
print(“Page fully loaded”)
Useful when waiting for the entire page DOM to finish loading.
Tip
- Prefer WebDriverWait over time.sleep() because it is smarter and also waits only as long as needed.
- Use both implicit and explicit waits cautiously. Explicit waits take priority when both are used.