NaiboWang/EasySpider · error · NoSuchElementException

No iframes found in the current page while searching for {va

Error message

No iframes found in the current page while searching for {value}

What it means

Raised by MyChrome.find_element (ExecuteStage/myChrome.py:100) as NoSuchElementException when iframe=True but driver.find_elements(By.CSS_SELECTOR, 'iframe') returns an empty list. It is the Python runtime mirror of error 1: a precondition guard before recursion.

Source

Thrown at ExecuteStage/myChrome.py:100

                except NoSuchElementException:
                    # Recurse into nested iframes
                    nested_frames = super(MyChrome, self).find_elements(By.CSS_SELECTOR, "iframe")
                    if nested_frames:
                        element = self.find_element_recursive(by, value, nested_frames)
                        if element:
                            return element
            except Exception as e:
                print(f"Exception while processing frame: {e}")

        raise NoSuchElementException(f"Element {value} not found in any frame or iframe")

    def find_element(self, by=By.ID, value=None, iframe=False):
        self.switch_to.default_content()  # Switch back to the main document
        self.iframe_env = False
        if iframe:
            frames = self.find_elements(By.CSS_SELECTOR, "iframe")
            if not frames:
                raise NoSuchElementException(f"No iframes found in the current page while searching for {value}")
            self.iframe_env = True
            element = self.find_element_recursive(by, value, frames)
        else:
            # Find element in the main document as normal
            element = super(MyChrome, self).find_element(by=by, value=value)
        return element

    # def find_elements(self, by=By.ID, value=None, iframe=False):
    #     # 在这里改变查找元素的行为
    #     if self.iframe_env:
    #         super().switch_to.default_content()
    #         self.iframe_env = False
    #     if iframe:
    #         # 获取所有的 iframe
    #         iframes = super().find_elements(By.CSS_SELECTOR, "iframe")
    #         find_element = False
    #         # 遍历所有的 iframe 并找到里面的元素
    #         for iframe in iframes:

View on GitHub (pinned to 191bd6d754)

Solutions

  1. Confirm the page has iframes at execution time (driver.find_elements length > 0).
  2. Add a wait for the iframe element before calling find_element with iframe=True.
  3. Drop iframe=True if the target is in the main document.
  4. Check for shadow-DOM hosted iframes and use JS-based discovery if needed.

Example fix

# before
el = driver.find_element(By.XPATH, xpath, iframe=True)

# after - guard the iframe path
if driver.find_elements(By.CSS_SELECTOR, 'iframe'):
    el = driver.find_element(By.XPATH, xpath, iframe=True)
else:
    el = driver.find_element(By.XPATH, xpath)
Defensive patterns

Strategy: validation

Validate before calling

frames = driver.find_elements(By.CSS_SELECTOR, 'iframe')
if iframe and not frames:
    # do not call find_element(..., iframe=True)
    pass

Try / catch

from selenium.common.exceptions import NoSuchElementException
try:
    el = driver.find_element(By.XPATH, xpath, iframe=True)
except NoSuchElementException as e:
    if 'No iframes found' in str(e):
        el = driver.find_element(By.XPATH, xpath)  # fall back to top doc
    else:
        raise

Prevention

When it happens

Trigger: driver.find_element(by, value, iframe=True) switches to default_content, queries self.find_elements(By.CSS_SELECTOR, 'iframe'); if the list is falsy it raises NoSuchElementException(f'No iframes found in the current page while searching for {value}').

Common situations: Task designed for an iframe page runs on a page variant with no iframe; iframe flag set by mistake for a top-document element; page not loaded; iframe injected via shadow DOM.

Related errors


AI-assisted analysis of NaiboWang/EasySpider@191bd6d754 (2026-08-13). Data as JSON: /api/errors/4f9c9e128e307295. Report an issue: GitHub.