🪟 Frames, Alerts & Multiple Windows
Frames, Alerts & Multiple Windows: An iframe is a separate DOM context embedded inside the main page — analogous to loading a second ClassLoader inside a Java ClassLoader: you ca
An iframe is a separate DOM context embedded inside the main page — analogous to loading a second ClassLoader inside a Java ClassLoader: you cannot reach inner elements with findElement from the outer context; you must call driver.switchTo().frame() first to enter that context. But if the page looks like a single visual surface, why is a separate context even necessary? Because the browser must enforce security and resource isolation for the iframe's origin; payment forms (Stripe, iyzico) and CAPTCHA widgets always live in their own iframe for exactly this reason. Alerts are a different category altogether: they are not DOM elements but native browser-level dialogs — if you do not capture them with driver.switchTo().alert() the entire test blocks indefinitely. Multiple-window management resembles tracking thread IDs with Java WeakReferences: each window handle is a String ID returned by driver.getWindowHandles(), and focus changes via switchTo().window(handle). The QA scenario that most reliably escapes detection is this: if the iframe context switch is missing on a payment page, the "Buy" button is found and clicked without error — the test returns a false PASS and the checkout flow is never actually tested.
1. Alert / Confirm / Prompt
Java — Alert Handling
Micro Lab: Selenium — Frame and Alert handling
Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.
Step by Step: The Difference Between alert.accept() and alert.dismiss()
First, wait with alertIsPresent()
An alert does not always appear INSTANTLY — `wait.until(ExpectedConditions.alertIsPresent())` waits until it is ready, otherwise a `NoAlertPresentException` is thrown.
`alert.accept()` simulates pressing the OK/Confirm button — for a `confirm()` dialog, this makes the JavaScript side receive `true`.
dismiss() presses Cancel
`alert.dismiss()` simulates pressing Cancel — for a `confirm()` dialog, JavaScript receives `false`.
sendKeys() only makes sense for prompt
`prompt.sendKeys("text")` only types into the input field of a `window.prompt()` dialog — calling it on `alert()` or `confirm()` fails because they have NO input field.
Python — Alert Handling