selenium no such element but element exists: debugging
Debugs Selenium NoSuchElementException when the element is visibly present: frames, timing, and locators. Use when you can see it but Selenium cannot. Not for stale elements.
TL;DR
If you see it and Selenium does not, the driver is looking in the wrong place: wrong frame, too early, or a subtly wrong locator. Check frames first, then timing, then the locator in the console.
Error
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"#submit"}Steps
- Check for frames:
driver.find_elements(By.TAG_NAME, "iframe"). If any,driver.switch_to.frame(...)before finding. Expected: the most common cause eliminated. - Add an explicit wait:
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "submit"))). Expected: timing issues resolve. - Test the locator in the browser console:
document.querySelector('#submit'). Expected: null means the locator is wrong, not the timing. - Check for shadow DOM: if the element is under
#shadow-root, pierce it with script. Expected: shadow boundary identified. - Check the locator scope: after any
switch_to, switch back withdriver.switch_to.default_content(). Expected: no stuck frame context.
When to use
- Element visible in screenshots but not found.
- After page changes involving frames.
When not to use
- Stale element (found then lost).
- Playwright (different engine).
Tool compatibility
- Selenium 4.x;
WebDriverWait, frame switching.
Variant phrasings
Unable to locate element in Selenium
The general form; frames-timing-locator in that order.
Selenium cannot find element that exists
Same symptom; same checklist.
Why it happens
The driver searches one document at a time, at one moment. Frames, async rendering, and shadow DOM each hide elements from a naive search.
Edge cases
- Implicit waits do not help inside frames you never switched to.
- Dynamically inserted iframes need a wait for the frame itself.
find_elements(plural) returning empty is easier to debug than the exception form.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_jjNeR6AEPqaWV88ZKW4kaw
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.