This article explains how to identify and count the number of iframes (inline frames) present on a webpage using Selenium WebDriver. Two methods are used:
- Locating all
- Executing JavaScript to dynamically calculate the number of frames.
An iFrame, or inline frame, is an HTML element that allows you to embed content from another source into a web page. iFrames are used to display content from other websites, such as videos, maps, advertisements, and social media feeds, directly on a web page.
Below is the snippet of the HTML code being used
File: iframe.html
Simple IFrames Example
Simple IFrames Example
Main Page Content
This is the main page content.
File iframe1.html
IFrame 1
IFrame 1: Greetings
Hello from IFrame 1!
File: iframe2.html
IFrame 2
IFrame 2: Message
Welcome to IFrame 2!
2. Create a Python file
- Import the necessary libraries
- Create an instance of the browser
- Navigate to the target URL

3. Finding all iframe elements using the

Used the find_elements with the tag name “iframe” to locate all iframe elements on the page and print the total count of iframe elements.
4. Using JavaScript to count the number of frames

Executed the JavaScript (return window.length) to calculate the number of frames dynamically present in the DOM, converted the result to an integer and printed the number.
And finally close the browser.
Final code
from selenium import webdriver
from selenium.webdriver.common.by import By
# Initialize WebDriver for Chrome
driver = webdriver.Chrome()
try:
# Navigate to the URL containing iFrames
driver.get("http://127.0.0.1:5500/iframe.html")
# Find all iframe elements using the
Output

With this you have learned how selenium handles the iframes.
Similar Posts
How to handles Drop down in selenium
How to Handle Alerts and Popups in Selenium
Locating Strategies in Selenium : Locating by Id
Installing Selenium and Configuring Web Driver
Selenium Python Example: How to run your first Test?