Consider the following Selenium Java code that switches to an iframe and retrieves text from an element inside it.
driver.switchTo().frame("frame1");
String text = driver.findElement(By.id("message")).getText();
System.out.println(text);The iframe with name "frame1" contains an element with id "message" and text "Hello Frame".
What will be printed?
driver.switchTo().frame("frame1"); String text = driver.findElement(By.id("message")).getText(); System.out.println(text);
Switching to the correct iframe allows access to elements inside it.
Since the code switches to the iframe named "frame1" correctly, it can find the element with id "message" inside it and print its text "Hello Frame".
You want to verify that after switching to an iframe, the current frame is the expected one by checking the frame's name attribute.
Which assertion is correct in Java with Selenium?
driver.switchTo().frame("frame2"); String frameName = (String) ((JavascriptExecutor) driver).executeScript("return window.name");
Check for exact match of the frame name.
The assertion assertEquals("frame2", frameName) verifies that the frame's window.name property exactly matches the expected iframe name.
Examine the code below:
driver.switchTo().frame(5);
WebElement element = driver.findElement(By.id("inside"));The page has only 3 iframes indexed 0 to 2.
Why does this code throw NoSuchFrameException?
Check the number of iframes on the page and the index used.
Using an index outside the range of available iframes causes NoSuchFrameException. Here, index 5 is invalid because only 3 iframes exist (0,1,2).
You want to switch to an iframe using a WebElement locator. Which option correctly locates the iframe element before switching?
Use a locator that uniquely identifies the iframe by its name attribute.
Using By.cssSelector("iframe[name='frame3']") precisely locates the iframe by its name attribute, ensuring the correct frame is switched to.
Which approach best ensures reliable iframe switching in a reusable test framework?
Waiting for iframe presence improves test stability.
Waiting for the iframe to be available before switching prevents errors and makes tests more stable and reusable.