Switch Between Frames and Default Content
Key frame navigation methods:
driver.switchTo().frame() - switch to a frame
driver.switchTo().parentFrame() - go up one level
driver.switchTo().defaultContent() - go back to the main page
Full Page Frame Structure:
<!-- Default Content (main page) -->
<h2>Frames</h2>
<iframe id="frame1"> <!-- switchTo().frame("frame1") -->
<h1>CodeKru</h1>
</iframe>
<iframe name="nestedFrame"> <!-- switchTo().frame("nestedFrame") -->
<h2>Parent frame</h2>
<iframe> <!-- switchTo().frame(0) from parent -->
<h3>Child frame</h3>
</iframe>
</iframe>
defaultContent() always returns to the main page. parentFrame() goes up one level.
Task:
- Navigate to
https://testkru.com/Interactions/Frames
- Get the main page heading
- Switch to frame1, get its h1 text
- Switch back to default and get the heading again to verify
Expected output:
Main page: Frames
Frame content: CodeKru
Back to main: Frames