In Selenium WebDriver, why do we need to switch context when working with iframes or multiple windows?
Think about how Selenium sees the web page structure and what happens when elements are inside frames or new windows.
Selenium interacts only with the current browsing context. If elements are inside an iframe or a new window, Selenium must switch to that context to find and interact with those elements.
Consider the following Selenium Java code snippet. What will be printed?
driver.get("https://example.com"); driver.switchTo().frame("frame1"); String text = driver.findElement(By.id("insideFrame")).getText(); System.out.println(text);
Assume the iframe 'frame1' and element with id 'insideFrame' exist on the page.
After switching to the iframe 'frame1', Selenium can find the element inside it and get its text. So the printed output is the text content of that element.
You have switched to a new browser window using Selenium. Which assertion correctly checks that the page title is "Welcome Page"?
driver.switchTo().window(newWindowHandle);
Remember the order of parameters in assertEquals(expected, actual) and how to compare strings in Java.
In JUnit, assertEquals expects the expected value first, then the actual value. Using equals() method is correct for string comparison. Option A follows this correctly.
Given the code below, the test fails with NoSuchElementException. What is the most likely reason?
driver.switchTo().frame("frame1"); driver.findElement(By.id("submitBtn")).click();
Check if the element is inside the iframe you switched to.
If the element is not inside the iframe, switching to that iframe will cause Selenium to look for the element in the wrong context, resulting in NoSuchElementException.
After switching to an iframe and performing actions, which code snippet correctly switches back to the main page context?
Consider the difference between defaultContent() and parentFrame() methods.
defaultContent() switches Selenium's focus back to the main document regardless of nesting. parentFrame() switches to the immediate parent frame, which may not be the main document if nested.