I’m working on automating tests for a private website using Python with Selenium WebDriver (both Firefox and Chrome browsers in headless mode). My challenge is capturing a complete screenshot of content that’s located inside nested iframes on the page.
The webpage structure has two levels of iframes, and I specifically need to screenshot everything from the deepest nested iframe. I’ve found various methods online for taking full page screenshots of regular web pages, but none of them address the iframe scenario.
My current setup involves switching to the iframe context, but I’m struggling with getting the complete content captured rather than just the visible portion. Has anyone dealt with this specific situation where you need to capture the entire scrollable content from within an iframe using Selenium?
Any code examples or guidance would be really helpful since this is blocking my automated testing workflow.
faced this too! after switching to the iframe, i found scrolling is a must to load all content. then use execute_script to get document.documentElement.scrollHeight - it really helps! especially in chrome, works kinda better. hope this helps!
try expandin your window size 1st before switchin to the iframe. use driver.set_window_size() for that. then switch to the nested iframe and grab the screenshot with get_screenshot_as_png(). this worked for me when viewport was too tiny to capture all content.
For nested iframe screenshots, timing is everything. Switch to your deepest iframe, then use driver.execute_script with “return document.readyState” to make sure it’s fully loaded before taking the screenshot. Most iframes load content async, so this check stops you from capturing half-loaded renders. I also add an explicit wait after switching contexts - WebDriverWait with presence_of_element_located works great. The big difference from normal screenshots is that iframe dimensions usually don’t match the parent window. You’ll need to grab the iframe’s actual content boundaries using getBoundingClientRect() through JavaScript. This method’s worked reliably for me across different iframe setups.
I fixed this by taking multiple screenshots and stitching them together with code. Switch to your nested iframe, grab the full content height with JavaScript, then figure out how many viewport screenshots you need. Take shots while scrolling through the iframe bit by bit, then use PIL to merge them into one complete image. This works way better than resizing the viewport since some iframes have size limits. Just make sure you pause between scrolls so lazy-loaded stuff can render. Works great in both Firefox and Chrome headless.
I’ve dealt with this exact problem when automating tests with complex iframe setups. Here’s what worked for me: combine iframe switching with viewport manipulation. Switch to the nested iframe with driver.switch_to.frame(), then use JavaScript to calculate the total content dimensions before taking the screenshot. I run driver.execute_script to grab both scrollWidth and scrollHeight from the iframe’s document, then resize the browser window to match. Here’s the tricky bit - some iframes have their own scrollbars, so you’ll need to scroll inside the iframe first to make sure everything loads. Firefox handles this way better than Chrome in headless mode, though both work if you configure them right.
Been there with nested iframe testing - it’s a nightmare. Manual scrolling and stitching work but break easily and crawl when you’re running hundreds of tests.
I ditched Selenium’s iframe mess and automated everything with Latenode instead. Built a scenario that handles browser automation, iframe navigation, and screenshot capture all in one flow.
Latenode orchestrates the whole thing - switches iframe contexts, calculates content dimensions, handles scrolling logic, and stitches images automatically. No more viewport resizing or manual JavaScript execution.
When test requirements change or you need different iframe structures, just modify the workflow instead of rewriting Python code. I use this for regression testing across multiple environments and it runs rock solid.
Way cleaner than juggling all those moving parts in Selenium.