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!!

    Switch Window Commands in Selenium WebDriver

    HOME

    One of the major testing workflows involves switching between multiple windows. Selenium WebDriver has specific switch commands to serve this purpose. Selenium WebDriver assigns an alphanumeric id to each window as soon as the WebDriver object is instantiated. This unique alphanumeric id is called a window handle. Selenium uses this unique id to switch control among several windows. In simple terms, each unique window has a unique ID, so that Selenium can differentiate when it is switching controls from one window to the other.

    1) GetWindowHandle

    To get the window handle of the current window. It returns a string of alphanumeric window handles.

    String  parentHandle= driver.getWindowHandle();
    

    2) GetWindowHandles

    To get the window handle of all the windows. It returns a set of window handle.

    Set  handle= driver.getWindowHandles();
    

    3) SwitchTo Window

    WebDriver supports moving between named windows using the “switchTo” method.

    driver.switchTo().window("windowName");
    

    Let us explain window switch with an example:-

    1) Launch new Browser and open https://demoqa.com/browser-windows
    2) Check the count of windows which is 1
    3) Locate “New Window” button using Id – “windowButton” and click to open a new window
    4) Get the count of both windows which is now 2.
    5) Get the parent window handle and print it to console
    6) Get the window handles of both the open windows and print them
    7) Switch to the new window (child window)
    8) Get the text of Child Window and print it
    9) Close the new window (child window)

    The program for the above scenario is shown below:

    package com.example.definitions;
    
    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;
    import java.util.Iterator;
    import java.util.Set;
    import java.util.concurrent.TimeUnit;
    
    public class WindowSwitchDemo {
    
        public static void main(String[] args) {
    
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--start-maximized");
            WebDriver driver = new ChromeDriver(options);
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    
            // Pass application url
            driver.get("https://demoqa.com/browser-windows");
    
            //Count of window - 1
            Set<String> allWindowHandles = driver.getWindowHandles();
            System.out.println("Count of Window :" + allWindowHandles.size());
    
            //Open a child window
            driver.findElement(By.id("windowButton")).click();
    
            //Count of windows , changed from 1 to 2
            Set<String> newAllWindowHandles = driver.getWindowHandles();
            System.out.println("New Count of Window :" + newAllWindowHandles.size());
    
            // Get the detail of the parent window
            String ParentHandle = driver.getWindowHandle();
            System.out.println("Parent Window :" + ParentHandle);
    
            //Get details of parent and child windows
            Iterator<String> iterator = newAllWindowHandles.iterator();
            String mainWindow = iterator.next();
            String childWindow = iterator.next();
            System.out.println("Parent Window :" + mainWindow);
            System.out.println("Child Window :" + childWindow);
    
            //Switch control to child window
            driver.switchTo().window(childWindow);
    
            //Verify the text present on child window
            WebElement text = driver.findElement(By.id("sampleHeading"));
            System.out.println("Child_Title :" + text.getText());
    
            // Close Child window
            driver.close();
    
            // Switch back to parent window
            driver.switchTo().window(ParentHandle);
            System.out.println("Parent Title :" + driver.getTitle());
    
            // Close Parent window
            driver.quit();
        }
    
    }
    

    What is the difference between driver.close() and driver.quit()?

    When we are working on multiple windows and a selective window needs to be closed, then transfer the control to that window and use driver.close() to close the selective window. This will not stop the execution of the rest of the program. But, in case it is needed to close all the open windows, then use driver.quit() which will close all the windows opened in a particular session. It basically stops the driver instance, and any further actions to WebDriver may result in an exception.  It is generally the last statement of any code.

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