How to Fix Selenium TimeoutException in Python
In this tutorial, you'll learn about How to Fix Selenium TimeoutException in Python. We cover key concepts, practical examples, and best practices.
Selenium raises TimeoutException when a WebDriverWait condition is not met within the specified timeout period.
The Wrong Way
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
driver = webdriver.Chrome()
driver.get("https://slow-site.example.com")
wait = WebDriverWait(driver, 5)
element = wait.until(EC.presence_of_element_located((By.ID, "data-table")))
This produces:
selenium.common.exceptions.TimeoutException: Message:
The Right Way
Increase the timeout and check the actual page state:
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
driver = webdriver.Chrome()
driver.get("https://slow-site.example.com")
wait = WebDriverWait(driver, 20)
try:
element = wait.until(EC.presence_of_element_located((By.ID, "data-table")))
print("Element found")
except TimeoutException:
print("Element not found within timeout")
print("Page title:", driver.title)
driver.save_screenshot("debug.png")
Step-by-Step Fix
1. Increase the timeout duration
# Change from 5 to 20 seconds
wait = WebDriverWait(driver, 20)
2. Check the poll frequency
# Default poll frequency is 0.5 seconds
# Increase for slow-loading pages
wait = WebDriverWait(driver, timeout=20, poll_frequency=1)
3. Verify the locator is correct
# Print the current page source to debug
print(driver.page_source[:2000])
# Or check if a simpler element exists
body = driver.find_element(By.TAG_NAME, "body")
print("Page loaded, body present")
4. Use a more lenient condition
# Instead of presence_of_element_located
wait.until(EC.visibility_of_element_located((By.ID, "data-table")))
# Or wait for any element matching a selector
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "table")))
5. Ignore specific exceptions during wait
from selenium.common.exceptions import StaleElementReferenceException
wait = WebDriverWait(
driver,
timeout=20,
ignored_exceptions=[StaleElementReferenceException]
)
Prevention Tips
- Start with a generous timeout (15-30 seconds) and adjust down.
- Use
driver.save_screenshot()to debug timeout issues. - Verify locators manually in the browser console.
- Use
presence_of_all_elements_locatedto find multiple elements. - Log page loading states to identify where the wait fails.
Common Mistakes with selenium timeout
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging
These mistakes appear frequently in real-world PYTHON code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro