Skip to the page

UI automation · mid

Frames, windows and shadow DOM

Normal searches do not go inside iframes, other windows or shadow roots. You must switch or search the right context first.

The notes

What you must know

  • Iframe: wait for frameToBeAvailableAndSwitchToIt with a stable locator (such as the title), act, then switchTo().defaultContent() or parentFrame().
  • Each card field may be its own iframe. Do not use switchTo().frame(0) by index.
  • Windows: keep the first getWindowHandle(), click, wait for numberOfWindowsToBe(2), switch to the new handle, act, close it, and switch back.
  • Shadow DOM hides a component inner DOM from normal searches. In Selenium 4: find the host, call getShadowRoot(), then findElement with CSS on it. Repeat for nested roots.
  • Use CSS inside shadow roots. XPath is not supported there in Chromium drivers.
  • Closed shadow roots cannot be reached by normal means. Ask for test hooks.
  • Playwright: frameLocator for iframes, page.waitForEvent("popup") started before the click for new tabs, and CSS, role and text locators pierce open shadow roots by default.
  • For real payment pages, use only the sandbox and test cards. Many teams test the full payment UI in a few sandbox tests and mock the gateway elsewhere.

The red pen

Common traps: mistake → fix

  • Mistake: Selenium cannot handle shadow DOM

    Fix: use getShadowRoot().

  • Mistake: Element not found means add a wait

    Fix: the element may be in another frame or shadow root, and no wait can fix that.

  • Mistake: After the frame, the page continues as normal

    Fix: you must switch back to the default content.

Easy to hard

Questions interviewers ask, easy to hard

  1. A card input is inside an iframe. How do you type into it?
  2. A click opens the bank page in a new tab. How do you handle it?
  3. How do you find an input inside a shadow root in Selenium?
  4. Why use sandbox cards for payment UI tests?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview