Skip to content
CodeKruPro

Switch into a frame by its name

Automation taskMediumAsked in: Accenture, Capgemini, Cognizant, Infosys, TCS, Wipro

"In how many ways can you switch to a frame?" Three: by index, by name or id, and by the frame element itself. This task uses the name, then returns to the top document.

Switch to Frame by Name

Switch to Frame by Name

You can also switch to a frame by its name attribute.

On the Frames page, the nested frame has name="nestedFrame" and contains a parent with <h2>Parent frame</h2>.

Page DOM Structure:

<iframe name="nestedFrame" src="...">
  <!-- Inside the parent frame: -->
  <body>
    <h2>Parent frame</h2>
    <iframe src="...">
      <!-- Child frame inside -->
      <h3>Child frame</h3>
    </iframe>
  </body>
</iframe>

Task:

  1. Navigate to https://testkru.com/Interactions/Frames
  2. Switch to frame with name nestedFrame
  3. Find the h2 element and print its text
  4. Switch back to default content

Expected output:

Parent frame text: Parent frame