Testing / Playwright Interview questions
How do you handle iframes in Playwright?
Playwright avoids the manual "switch into frame, then switch back" pattern that other tools require. Instead, page.frameLocator() scopes a locator chain to the contents of a specific iframe, and you can immediately query inside it.
const frame = page.frameLocator('#payment-iframe'); await frame.getByLabel('Card number').fill('4242424242424242');
Because frameLocator itself is lazy like a regular locator, it automatically waits for the iframe to exist and re-resolves it if the iframe is removed and re-added - for example, when a payment widget reloads. There's no need to call anything like "switchTo" before or "switchToDefault" after; actions outside the frame just use page as normal.
Nested iframes work the same way - chaining another frameLocator() call scopes further into an iframe within an iframe, and each level continues to auto-wait independently of the others.
More Related questions...