Skip to content

How to Fix Selenium TimeoutException in Python

DodaTech Updated 2026-06-24 2 min read

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_located to find multiple elements.
  • Log page loading states to identify where the wait fails.

Common Mistakes with selenium timeout

  1. Misunderstanding that String is [Char] with poor performance for large text operations
  2. Using foldl instead of foldl' causing stack overflow on large lists
  3. 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

### What does TimeoutException mean?

It means Selenium waited for a condition (like an element appearing) for the full timeout duration, and the condition never became true. Either the timeout is too short, the locator is wrong, or the page behaved unexpectedly.

Can slow network cause TimeoutException?

Yes. If the page assets take longer to load than the timeout allows, element conditions may not be met. Increase the timeout or wait for document.readyState to be 'complete' before searching.

How do I set a global timeout for all waits?

Use driver.implicitly_wait(seconds) to set a default timeout for all element searches. Note that mixing implicit and explicit waits can cause unpredictable behavior -- stick to explicit waits.

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro