These three scenarios trip up every beginner. A JavaScript alert pops up and your findElement() can't close it. An element is inside an iframe and Selenium can't see it. A link opens a new tab and your code is still on the old one. Let's handle all three.
Alerts are NOT part of the HTML page. You can't findElement() on them. You have to switch to them using driver.switchTo().alert().
import org.openqa.selenium.Alert;
// Switch to the alert
Alert alert = driver.switchTo().alert();
// Read the alert message
String alertText = alert.getText();
System.out.println("Alert says: " + alertText);
// Click OK
alert.accept();
// Click Cancel (for confirm dialogs)
alert.dismiss();
// Type into a prompt dialog
alert.sendKeys("my input");
alert.accept();
// Safe approach — handle alert only if it exists
try {
driver.switchTo().alert().accept();
} catch (org.openqa.selenium.NoAlertPresentException e) {
// No alert — move on
}An iframe is a webpage inside a webpage. Think of it like a TV screen inside a TV screen. Selenium can only see elements on the "screen" it's currently on. If an element is inside an iframe, you have to switch into that iframe first.
// Switch to iframe by index (0 = first iframe on the page)
driver.switchTo().frame(0);
// Switch by name or id attribute
driver.switchTo().frame("paymentFrame");
// Switch by WebElement
WebElement iframe = driver.findElement(
By.cssSelector("iframe.payment-gateway"));
driver.switchTo().frame(iframe);
// NOW you can interact with elements inside the iframe
WebElement cardNumber = driver.findElement(By.id("cardNumber"));
cardNumber.sendKeys("4111111111111111");
// Switch back to the main page
driver.switchTo().defaultContent();
// Or go up one level (if nested iframes)
driver.switchTo().parentFrame();After switching into an iframe, EVERY findElement() call looks inside that iframe — not the main page. Always switch back to defaultContent() when you're done. Forgetting this is the #1 iframe mistake.
// Save the current window handle BEFORE the new window opens
String mainWindow = driver.getWindowHandle();
// Click something that opens a new tab
driver.findElement(By.linkText("Open Account")).click();
// Get ALL window handles
Set<String> allWindows = driver.getWindowHandles();
// Switch to the new window
for (String handle : allWindows) {
if (!handle.equals(mainWindow)) {
driver.switchTo().window(handle);
System.out.println("New window: " + driver.getTitle());
}
}
// Do your work in the new window...
// Close the new window and go back to main
driver.close(); // Closes current (new) window only
driver.switchTo().window(mainWindow); // Back to mainAlways save the main window handle BEFORE clicking the link that opens a new window. If you try to get it after, you won't know which handle is which.
Q: How do you handle alerts in Selenium?
A: Use driver.switchTo().alert() to get the Alert object. Then accept() clicks OK, dismiss() clicks Cancel, getText() reads the message, and sendKeys() types into a prompt. If no alert is present, it throws NoAlertPresentException — I wrap it in a try-catch for safe handling.
Q: How do you handle iframes?
A: Elements inside an iframe are invisible to Selenium until you switch into it. I use driver.switchTo().frame() — passing the index, name/id, or WebElement. After interacting with elements inside the iframe, I switch back with driver.switchTo().defaultContent(). The most common mistake is forgetting to switch back, which causes NoSuchElementException for elements on the main page.
Q: How do you handle multiple windows?
A: Before the new window opens, I save the main window handle with getWindowHandle(). After the click, I use getWindowHandles() to get all handles, loop through them, and switch to the one that's not the main window. When done, I close the new window with close() and switch back to the main window with switchTo().window(mainWindowHandle).
Key Point: Alerts: switchTo().alert(). Frames: switchTo().frame() then defaultContent(). Windows: save the main handle, loop to find the new one, switch back when done.