Selenium WebDriver

Frame Handling in Selenium

Master frame handling techniques including switching between frames, nested frames, and frame navigation using index, ID, name, and WebElement.

Frame Handling in Selenium

What are Frames?

Frame is used to divide the browser window into multiple sections and each section carries separate HTML documents. If we try to perform operations on frame elements without switching to it, we get NoSuchElementException.

⚠️ Important

If you try to perform operations on frame elements without switching to the frame first, you will get NoSuchElementException.

🔍 How to Identify Frames

Check if frames exist in the application by looking for <frame> and <iframe> tags in HTML DOM Structure.

Frame Switching Methods

We switch focus from main window to frame window using four different approaches:

1. By Frame Index

Frame index starts from top-left to bottom-right

driver.switchTo().frame(0);

2. By Frame ID

Using the id attribute of frame

driver.switchTo().frame("frameId");

3. By Frame Name

Using the name attribute of frame

driver.switchTo().frame("frameName");

4. By Frame WebElement

Using WebElement reference

driver.switchTo().frame(webElement);

📝 Important Notes

  • • Cannot switch from one frame to another frame directly
  • • Must first switch to top frame/parent frame
  • • All frame methods return WebDriver interface

Frame Navigation Methods

defaultContent() Method

Switch focus from frame window to top window (main window)

driver.switchTo().defaultContent();

parentFrame() Method

Switch focus from frame window to immediate parent window (frame or main window)

driver.switchTo().parentFrame();

Practical Examples

Example 1: Single Frame Handling

URL: https://prafpawar11.github.io/mainFrame.html

Task: Enter value in first name text box and select Selenium checkbox

package Tutorial7;

import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;

public class Demo1 {

    public static void main(String[] args) {

        ChromeDriver driver = new ChromeDriver();
        driver.get("https://prafpawar11.github.io/mainFrame.html");

        // Enter value in main window text box
        driver.findElement(By.id("name")).sendKeys("Rahul");

        // Switch focus to checkbox frame
        driver.switchTo().frame("mainframe");

        // Find and select Selenium Checkbox
        driver.findElement(By.id("Selenium")).click();
    }
}

Example 2: Multiple Frames Handling

URL: https://prafpawar11.github.io/twoFrame.html

Task: Enter full name, select Cucumber checkbox, and select Jenkins from dropdown

package Tutorial7;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class Demo2 {

    public static void main(String[] args) {

        ChromeDriver driver = new ChromeDriver();
        driver.get("https://prafpawar11.github.io/twoFrame.html");

        // Enter value in main window
        driver.findElement(By.name("name")).sendKeys("Rahul");

        // Switch to first frame (index 0)
        driver.switchTo().frame(0);
        driver.findElement(By.id("Cucumber")).click();

        // Switch back to main window
        driver.switchTo().defaultContent();

        // Switch to second frame
        driver.switchTo().frame("topic");

        // Handle dropdown
        WebElement wb = driver.findElement(By.name("course"));
        Select sel = new Select(wb);
        sel.selectByVisibleText("Jenkins");
    }
}

Example 3: Nested Frames Handling

URL: https://prafpawar11.github.io/frame.html

Task: Enter name, select TestNG checkbox, and enter address (nested frames)

package Tutorial7;

import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;

public class Demo3 {

    public static void main(String[] args) {

        ChromeDriver driver = new ChromeDriver();
        driver.get("https://prafpawar11.github.io/frame.html");

        // Enter name in main window
        driver.findElement(By.id("name")).sendKeys("Anjali");

        // Switch to address frame
        driver.switchTo().frame(0);

        // Switch to nested checkbox frame
        driver.switchTo().frame(0);
        driver.findElement(By.id("TestNG")).click();

        // Switch back to parent frame (address frame)
        driver.switchTo().parentFrame();
        driver.findElement(By.id("add")).sendKeys("Pune");
    }
}

Frame Navigation Flow

Navigation Rules

❌ Cannot Do

Cannot switch directly from one frame to another frame. Must first switch to parent/top window.

✅ Correct Approach

Step 1: Switch to Frame A
Step 2: Perform operations in Frame A
Step 3: Switch back to main window (defaultContent)
Step 4: Switch to Frame B
Step 5: Perform operations in Frame B

🔄 Navigation Methods Comparison

defaultContent()

Switches to top-level window (main window)

parentFrame()

Switches to immediate parent frame

Practice Assignments

🎯 Assignment 1: HYR Tutorials

URL: https://www.hyrtutorials.com/p/frames-practice.html

Frame 1 Tasks:
  • • Select Contact dropdown values
  • • Select Alerts value from dropdown
  • • Click on "Click Me" button
  • • Capture alert text and click OK
Frame 2 Tasks:
  • • Select Basic Control dropdown value
  • • Enter first name and last name
  • • Select gender and language
  • • Enter email address

🎯 Assignment 2: Nested iFrames

URL: https://demo.automationtesting.in/Frames.html

  • • Enter value in iframe Demo text box
  • • Switch to main window
  • • Click on "iframe with an iframe"
  • • Switch to nested frame → switch to iframe → enter value

🎯 Assignment 3: Complex Frame Navigation

URL: https://praf002.github.io/

  • • Enter full name and select GitHub checkbox
  • • Select Database Automation from dropdown
  • • Enter mobile number and address
  • • Unselect GitHub checkbox
  • • Click on Selenium, enter new name
  • • Select JavaScript Executor from dropdown
  • • Enter new address

Day 17 Knowledge Check

Question 1 of 5

What exception do you get when trying to interact with frame elements without switching to the frame?

SDET Mastery

Master Test Automation

Home
CurriculumPracticeQ&ACheatsheet
🍵Buy me a Chai

Automation Testing Course

Comprehensive course covering Manual Testing, Java Programming, and Selenium WebDriver

🍵Buy me a Chai
Privacy Policy•GitHub
© 2024 Automation Testing Course. All rights reserved.