How to Fix Selenium Element Not Found in Python
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
WebDriverWaitwithexpected_conditionsfor robust automation. - Add logging to track which page state is reached before failures.
Common Mistakes with selenium element
- Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging - 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
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro