Nested frames in Selenium Java - Build an Automation Script
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.NoSuchFrameException; import org.openqa.selenium.NoSuchElementException; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import java.time.Duration; public class NestedFramesTest { private WebDriver driver; private WebDriverWait wait; @BeforeEach public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); } @Test public void testNestedFrameText() { driver.get("https://example.com/nested_frames_page"); try { // Wait for outer frame and switch wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.name("frame1"))); // Wait for inner frame and switch wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.name("frame2"))); // Wait for paragraph element inside inner frame WebElement nestedText = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("nestedText"))); // Assert the text content assertEquals("Nested Frame Content", nestedText.getText(), "Text inside nested frame did not match."); } catch (NoSuchFrameException e) { throw new AssertionError("Frame not found: " + e.getMessage()); } catch (NoSuchElementException e) { throw new AssertionError("Element not found inside frame: " + e.getMessage()); } finally { // Switch back to default content driver.switchTo().defaultContent(); } } @AfterEach public void tearDown() { if (driver != null) { driver.quit(); } } }
This test opens the web page with nested frames.
It waits explicitly for the outer frame named 'frame1' to be available and switches to it.
Then it waits for the inner frame named 'frame2' inside the outer frame and switches to it.
Inside the inner frame, it waits for the paragraph element with id 'nestedText' to be visible.
It asserts that the text of this paragraph exactly matches 'Nested Frame Content'.
Finally, it switches back to the main page content to avoid side effects for other tests.
Setup and teardown methods initialize and close the browser properly.
Explicit waits ensure the frames and elements are ready before interacting, preventing flaky tests.
Now add data-driven testing with 3 different nested frame URLs and expected texts
Practice
Solution
Step 1: Understand frame switching
To access nested frames, Selenium requires switching into each frame one by one usingswitchTo().frame().Step 2: Confirm correct method usage
defaultContent()returns to the main page,parentFrame()goes up one level, andwindow()switches browser windows, not frames.Final Answer:
Usedriver.switchTo().frame()step-by-step for each frame level. -> Option AQuick Check:
Step-by-step frame switching = C [OK]
- Trying to switch directly to nested frame with defaultContent()
- Using window() instead of frame() for frames
- Confusing parentFrame() with entering nested frames
Solution
Step 1: Identify correct method signature
Theframe()method accepts a String for frame name/id, so it must be in quotes.Step 2: Check syntax correctness
Onlydriver.switchTo().frame("frame1");uses correct Java syntax with double quotes and parentheses.Final Answer:
driver.switchTo().frame("frame1"); -> Option BQuick Check:
Correct syntax uses quotes and parentheses = A [OK]
- Omitting quotes around frame name
- Using square brackets or curly braces instead of parentheses
- Passing variable without declaration
driver.switchTo().frame("outerFrame");
driver.switchTo().frame("innerFrame");
String text = driver.findElement(By.id("message")).getText();
System.out.println(text);Assuming the element with id "message" inside "innerFrame" contains "Hello Nested Frames".
Solution
Step 1: Switch to outer then inner frame
The code correctly switches first to "outerFrame", then to "innerFrame" where the element exists.Step 2: Locate element and get text
Since the element with id "message" is inside "innerFrame",getText()returns "Hello Nested Frames".Final Answer:
"Hello Nested Frames" -> Option AQuick Check:
Correct frame switching finds element text = D [OK]
- Not switching to inner frame before accessing element
- Trying to find element in default content
- Confusing exception types
driver.switchTo().frame("innerFrame");
String text = driver.findElement(By.id("msg")).getText();Solution
Step 1: Understand nested frame switching
To access a nested frame, you must first switch to its parent frame before switching to the inner frame.Step 2: Identify missing step
The code tries to switch directly to "innerFrame" without switching to its parent frame, causing failure to find the frame.Final Answer:
It misses switching to the parent frame before switching to innerFrame. -> Option DQuick Check:
Must switch parent frame first = B [OK]
- Skipping parent frame switch
- Passing frame name incorrectly
- Ignoring frame switching altogether
frameA contains frameB, which contains frameC. You want to click a button inside frameC. Which sequence of Selenium Java commands correctly switches to frameC and clicks the button with id "btnSubmit"?Solution
Step 1: Switch through each nested frame in order
SinceframeCis insideframeB, which is insideframeA, you must switch step-by-step: first toframeA, thenframeB, thenframeC.Step 2: Locate and click the button inside the innermost frame
After switching toframeC, find the button by id "btnSubmit" and click it.Final Answer:
driver.switchTo().frame("frameA"); driver.switchTo().frame("frameB"); driver.switchTo().frame("frameC"); driver.findElement(By.id("btnSubmit")).click(); -> Option CQuick Check:
Stepwise frame switching then action = A [OK]
- Trying to switch directly to innermost frame
- Using defaultContent() in middle of switching
- Not switching frames at all before clicking
Ready to prove it?
Pass the test and this pattern moves unknown
