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
2. By Frame ID
Using the id attribute of frame
3. By Frame Name
Using the name attribute of frame
4. By Frame WebElement
Using WebElement reference
📝 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)
parentFrame() Method
Switch focus from frame window to immediate parent window (frame or main window)
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
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
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)
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
🔄 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