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.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe 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
- A card input is inside an iframe. How do you type into it?
- A click opens the bank page in a new tab. How do you handle it?
- How do you find an input inside a shadow root in Selenium?
- 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 voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.