Task Progress0 of 5 Tasks
0%

Frames & Shadow DOM

Isolated contexts: iframes, nested iframes and shadow roots. Each element shows its goal beside it, and its task ticks itself the moment your script gets it right. New tabs and popup windows have their own page: Windows & Tabs.

1.Frames (iframe)

Switch into an iframeTo do

Access code: ACCESS-3416

Goal

Read the access code on the main page, switch into the frame, type the code there and press Submit.

Back to the main pageTo do

Solve the frame task first

Goal

After the frame task, switch back to the main document and click “Confirm on main page”.

Nested iframeTo do
Goal

Read the code inside the inner frame (a frame inside a frame) and type it in the answer box.

2.Shadow DOM

Open shadow rootTo do
Goal

Click the button inside the shadow root.

Nested shadow rootsTo do
Goal

Read the code from the shadow root inside another shadow root and type it in the answer box.

About this Frames & Shadow DOM page

An iframe is a separate document embedded in a page, and a shadow DOM is an encapsulated element tree attached to a host element. Test tools cannot see inside either by default. To automate them, switch to the frame first (switchTo().frame in Selenium, frameLocator in Playwright), then locate shadow content with a shadow root API or a piercing selector.

Screenshot of the Frames & Shadow DOM practice page on QA Playground. Switching contexts into iframes and shadow roots.
Frames & Shadow DOM: Switching contexts into iframes and shadow roots.

Frequently asked questions

How do I switch to an iframe in Selenium?

Call driver.switchTo().frame() with an index, a name or id, or the iframe WebElement. Locate elements inside it as usual, then return with switchTo().defaultContent(). Forgetting to switch back is the most common cause of NoSuchElementException on the main page.

How do I handle an iframe in Playwright?

Use page.frameLocator('iframe#id') and chain locators from it, for example frameLocator('#pay').getByRole('button'). Playwright waits for the frame and auto-waits for elements, so no manual switching or switching back is needed.

How do I find elements inside a shadow DOM in Selenium 4?

Find the shadow host, call getShadowRoot() on it (Java) or shadow_root (Python), then use findElement with a CSS selector on that root. XPath does not work inside shadow roots. For nested roots, repeat the step per host.

Does Playwright pierce shadow DOM automatically?

Yes. Playwright CSS and role-based locators such as getByRole, getByText and locator('css') pass through open shadow roots at any depth. XPath selectors do not pierce them, so prefer CSS or user-facing locators.

How do I test an iframe or shadow DOM in Cypress?

Cypress has no built-in frame switching. Read the iframe body from its contentDocument and wrap it, or use a plugin. For shadow DOM, pass { includeShadowDom: true } to cy.get or set it in config so selectors pierce open roots.

Free developer tools on Randomly.online

QA Playground is part of Randomly.online. These tools help while you write and debug tests: