How to Switch Between Frames in Selenium WebDriver

HOME

Switching between frames in Selenium can be necessary when dealing with web pages that use iframes. The methods to switch between frames allow you to interact with elements within those frames

1) Switching by Name or ID

If the frame or iframe has an id or name attribute, we can switch the frames using name or ID. If the name or ID is not unique on the page, then the first one found will be switched to.

//switch To IFrame using name or id
driver.findElement(By.name("iframe1-name"));

//Switch to the frame
driver.switchTo().frame(iframe);

2) Switching by WebElement

We can find the frame using any selector and switch to it.

WebElement frameElement = driver.findElement(By.id("frameId"));
driver.switchTo().frame(frameElement);

3) Switching by Index

Switching between the frames can be done by Index also.

//switch To IFrame using index
driver.switchTo().frame(0);
//leave frame
driver.switchTo().defaultContent();

Let us explain frame switchching with an example:-

1) Launch new Browser and open https://demoqa.com/frames
2) Switch iFrame using any of locator strategy
3) Switch back to main content
4) Switch iFrame using index
5) Close the window

The program for the above scenario is shown below:

package com.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;


public class iFrame_Demo {

    public static void main(String[] args) {

        ChromeOptions options = new ChromeOptions();
        WebDriver driver = new ChromeDriver(options);
        driver.manage().window().maximize();
        driver.get("https://demoqa.com/frames");

        //Switch iFrame using any of locator strategy
        WebElement iframeElement = driver.findElement(By.id("frame1"));
        driver.switchTo().frame(iframeElement);
        String Frame_1 = driver.findElement(By.id("sampleHeading")).getText();
        System.out.println("Switch by locator:" + Frame_1);

        //Switch back to the main window
        driver.switchTo().defaultContent();
        String mainPage = driver.findElement(By.xpath("//*[@id='framesWrapper']/h1")).getText();
        System.out.println("Back to Main page :" + mainPage);

        //Switch iFrame using index
         driver.switchTo().frame(1);
        String Frame_2 = driver.findElement(By.id("sampleHeading")).getText();
        System.out.println("Switch by Index :" + Frame_2);

        //quit the browser
        driver.quit();
    }
}

    Congratulations. We have learnt about window switching in Selenium. I hope you find this tutorial helpful. Happy Learning!!

    Basic Selenium Tutorials

    HOME

    Selenium – Introduction, Installation, Test Script

    Chapter 1 Introduction to Selenium Automation Tool
    Chapter 2 How to Download & Install Java JDK 11 in Windows
    Chapter 3 How to Download and Install Eclipse IDE
    Chapter 4 How to install IntelliJ on Windows
    Chapter 5 How to Download & Install Selenium WebDriver 
    Chapter 6  How to create first Selenium WebDriver Script using Java
    Chapter 7 How to run Selenium Tests using on Internet Explorer

    Locators in Selenium

     Chapter 1 How to Locate Elements in Chrome, Firefox and IE Browsers for creating Selenium Scripts
    Chapter 2 Locators in Selenium – Locate by ID, ClassName,  Name, TagName,  LinkText, PartialLinkText
    Chapter 3 Dynamic XPath  in Selenium WebDriver
    Chapter 4 CSS Selector in Selenium WebDriver

    Launching Browsers and headless Browser

    Chapter 1 How to run Chrome tests in headless mode in Selenium
    Chapter 2 How to run Firefox tests in headless mode in Selenium
    Chapter 3 How to run Edge tests in headless mode in Selenium4
    Chapter 4 How to manage driver executables using WebDriverManager
    Chapter 5 How to disable infobar warning for Chrome tests in Selenium
    Chapter 6 How to maximize and minimize the window in Selenium

    WebDriver Commands

    Chapter 1 Difference between FindElement and FindElements in WebDriver
    Chapter 2 Difference between getText() and getAttribute() method in WebDriver
    Chapter 3 WebDriver Browser Commands – get,  getTitle, getCurrentUrl, getPageSource, getClass, close, quit in WebDriver
    Chapter 4 WebDriver Navigation Commands – Navigate, Forward, Back, Refresh in  WebDriver
    Chapter 5 Selenium Form WebElement Commands – Sendkeys, Clear, Click,Submit
    Chapter 6 How to automate selecting Checkbox and Radio Buttons in Selenium WebDriver
    Chapter 7 How to Select value from Drop down list or perform Multiple Selection  Operations in WebDriver
    Chapter 8 How to get all options in a DropDown list in WebDriver
    Chapter 9 How to automate Radio Button in WebDriver
    Chapter 10 How to automate BootStrap DropDown using WebDriver
    Chapter 15 How to handle Dynamic Web Tables using Selenium WebDriver
    Chapter 16 How to get all the values from a Dynamic Table in Selenium WebDriver 
    Chapter 17 isDisplayed, isSelected, isEnabled in Selenium
    Chapter 18 How to test HTML ordered list in Selenium
    Chapter 19 How to test HTML5 validation messages with Selenium

    Waits in Selenium

    Chapter 1 Implicit and Explicit Wait in Selenium WebDriver
    Chapter 2 What is Fluent Wait in Selenium WebDriver

    Handle Window and Alerts

    Chapter 1 Switch Window Commands in Selenium WebDriver
    Chapter 2 How to handle Alerts in Selenium WebDriver
    Chapter 3 How to Switch Between Frames in Selenium WebDriver

    Selenium Interview Questions and Answers
    Advanced Selenium Interview Questions and Answers
    Selenium Multiple Choice Questions – MCQ1
    Selenium Multiple Choice Questions – MCQ1
    Selenium Multiple Choice Questions – MCQ3