Switch to Frame by ID
Frames (iframes) are separate HTML documents embedded in a page. To interact with elements inside a frame, you must first switch to it.
driver.switchTo().frame("frameId") switches to a frame by its ID.
On the Frames page, the first frame has id="frame1" and contains an <h1> with text "CodeKru".
Page DOM Structure:
<!-- Main page content -->
<h2>Frames</h2>
<iframe id="frame1" srcdoc="...">
<!-- Separate document inside frame1: -->
<body>
<h1>CodeKru</h1>
</body>
</iframe>
You must switchTo().frame() before you can access elements inside the iframe.
Task:
- Navigate to
https://testkru.com/Interactions/Frames
- Switch to the frame with ID
frame1
- Find the h1 element and print its text
- Switch back to default content
Expected output:
Frame content: CodeKru