Skip to content

How to Fix Selenium Element Not Found in Python

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about How to Fix Selenium Element Not Found in Python. We cover key concepts, practical examples, and best practices.

Selenium raises NoSuchElementException when the requested element is not present on the page at the time of the search.

The Wrong Way

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://example.com")
button = driver.find_element(By.ID, "submit")
button.click()

This produces:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="submit"]"}

The Right Way

Wait for the element to be present before interacting:

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://example.com")

wait = WebDriverWait(driver, 10)
button = wait.until(EC.presence_of_element_located((By.ID, "submit")))
button.click()

Step-by-Step Fix

1. Use explicit waits

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, "my-button")))
element.click()

2. Check for iframes

# Switch to iframe before searching
iframe = driver.find_element(By.TAG_NAME, "iframe")
driver.switch_to.frame(iframe)
element = driver.find_element(By.ID, "inside-iframe")
driver.switch_to.default_content()  # switch back

3. Verify the page is fully loaded

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((By.TAG_NAME, "body")))

4. Use multiple locator strategies

locators = [
    (By.ID, "submit"),
    (By.CSS_SELECTOR, "button[type='submit']"),
    (By.XPATH, "//button[text()='Submit']"),
]

for by, value in locators:
    try:
        element = driver.find_element(by, value)
        break
    except NoSuchElementException:
        continue

Prevention Tips

  • Always use explicit waits instead of implicit waits.
  • Verify element locators in the browser developer tools first.
  • Switch to the correct iframe or window before searching.
  • Use WebDriverWait with expected_conditions for robust automation.
  • Add logging to track which page state is reached before failures.

Common Mistakes with selenium element

  1. Using foldl instead of foldl' causing stack overflow on large lists
  2. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  3. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable

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 is the difference between implicit and explicit waits?

Implicit waits set a global timeout for all element searches. Explicit waits set a timeout for a specific condition. Use explicit waits for precise control and implicit waits as a fallback.

How do I find the right locator for an element?

Open the browser's developer tools, inspect the element, and choose a stable attribute like id, data-testid, or a unique CSS selector. Avoid fragile locators like auto-generated classes or index-based XPath.

Can Selenium find elements that are not visible?

Yes. presence_of_element_located finds elements in the DOM regardless of visibility. Use visibility_of_element_located or element_to_be_clickable to wait for visible elements.

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

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro