Handling iFrames in Playwright with TypeScript


The iFrames are used in web pages to load external content, such as ads, videos, or embedded forms. Automating such elements can be tricky because they act as separate pages inside the main page. Playwright provides powerful tools to interact with iframes easily.
Understanding Frames and iFrames Frames and iframes allow embedding external documents or content into a webpage. This can pose challenges for automation because traditional locators may not directly interact with elements inside frames. Playwright’s frame-handling capabilities provide robust solutions to overcome these hurdles.
Getting Started with Frames in Playwright Ensure you have Playwright set up in your TypeScript project. If not, install it using:
npm install @playwright/test
Then, create a basic test file for frame handling:
import { test, expect } from '@playwright/test';
const url = 'https://example.com/page-with-iframe'; // URL with iframe
test('Interact with iframe content', async ({ page }) => {
await page.goto(url);
const frame = page.frame({ name: 'iframe_name' }); // Locate the iframe by its name, id, or other attributes
if (frame) {
await frame.fill('input[name="username"]', 'testuser'); // Perform actions within the iframe
await frame.fill('input[name="password"]', 'password123');
await frame.click('button[type="submit"]');
const successMessage = frame.locator('text=Login successful'); // Assertion inside the iframe
await expect(successMessage).toBeVisible();
} else {
throw new Error('Frame not found');
}
});
Interacting with Nested Frames Sometimes, frames are nested within other frames. Playwright allows you to switch context to child frames easily:
test('Handle nested frames', async ({ page }) => {
await page.goto(url);
const parentFrame = page.frame({ name: 'parent_frame' });
const childFrame = parentFrame?.childFrames()[0];
if (childFrame) {
await childFrame.click('button#nestedButton');
const result = childFrame.locator('text=Action completed');
await expect(result).toBeVisible();
}
});
Handling Dynamic Frames Frames may load dynamically after the page has loaded. To wait for frames to load, use:
await page.waitForFunction(() => window.frames.length > 0);
Or explicitly wait for a specific frame:
await page.waitForSelector('iframe#dynamicFrame');
const dynamicFrame = page.frame({ name: 'dynamicFrame' });
Debugging Frame Issues
Use
page.frames()
to list all frames on the page.Print frame URLs to identify the target frame:
console.log(page.frames().map(frame => frame.url()));
Conclusion Mastering iframe handling in Playwright is essential for automating complex web applications, as iframes are commonly used for embedding third-party content, payment gateways, and more. By utilizing Playwright’s powerful frame management features, you can seamlessly interact with elements inside iframes, handle nested structures, and debug issues effectively. With these techniques, you can build more reliable and maintainable test scripts, ensuring robust automation across different web applications.
Subscribe to my newsletter
Read articles from NonStop io Technologies directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

NonStop io Technologies
NonStop io Technologies
Product Development as an Expertise Since 2015 Founded in August 2015, we are a USA-based Bespoke Engineering Studio providing Product Development as an Expertise. With 80+ satisfied clients worldwide, we serve startups and enterprises across San Francisco, Seattle, New York, London, Pune, Bangalore, Tokyo and other prominent technology hubs.