Back to All Scenarios
Scenario 39 of 100
Frames
Beginner
Checking the Total Count of Frames on a Page
π’Scenario Overview
Checking the Total Count of Frames on a Page
Key Takeaways & Cheat Sheet
- βRecognize that frames are represented by standard iframe or frame tags
- βRetrieve frame count using driver.findElements(By.tagName("iframe"))
- βAssert total list size to verify correct dynamic panel creation
- βAvoid iterating frames directly without resetting focus contexts
Short Direct Answer
To count how many frames are present on a page, fetch a list of all elements with the `iframe` or `frame` tags using `driver.findElements(By.tagName("iframe"))` and record the size of the returned collection. Always ensure your driver context is at the default page content root before querying.
β οΈ Senior Warning (Red Flag)
Do not use driver.findElements() inside a frame to count page frames. The search scope is restricted to that active frame. Switch to defaultContent() first.
π‘ STAR Deep Dive Explanation & Pro Tip
Counting frames is useful for debugging and dynamic site scraping validation to ensure overlays are fully rendered.
SeleniumAutomation.java
Selenium 4 + Java// β
Reset context to parent document to capture all frames
driver.switchTo().defaultContent();
// β
Fetch all iframe element handles on the page
List<WebElement> iframes = driver.findElements(By.tagName("iframe"));
int totalFrames = iframes.size();
System.out.println("Total iframes detected: " + totalFrames);