Switching Iframes – Selenium Webdriver Training

Welcome to Selenium Webdriver Free Training. Last time, we dug out the following points.

  1. What types of alerts?
  2. How to manage alerts?
  3. What are the different methods for different alerts?
  4. Selenium script for each alert.

If you missed or didn’t get time to read, here is a link to get the information about the points.

Alright. Let’s move the iframes.

What are the iframes?

Iframes are elements of a webpage that can be used to embed or add another webpage to the current page.

How to work with iframes?

We are going to test some drag and drop actions with selenium script, and these actions we’ll perform on the iframe window.

Selenium Webdriver Script Actions

Here are the tasks that we need to perform by selenium script.

1. Open http://jqueryui.com/droppable/

2. focus on the iframe window.

3. Get the references of drag and drop elements and assign them to respective variables.

4. Perform the drag and drop operation.

5. Get the focus back to the main window.

6. Close the browser.

Live script demo

Here is a demo to easily understand

In last tutorial, we enlightened on the in-built method switchTo(), this method will help to focus on the iframe, and then you can perform drag and drop operation. To get focus back to main window, we need to call  driver.switchTo().defaultContent(). 

Webdriver Script to manage iframes

Alright, let’s see the code:

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

public class ManageIframe {
    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver", "c:\\xampp\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        // focussing inside the iframe
        // using xpath
        WebElement source = driver.findElement(By.xpath("//div[@id='draggable']"));
        WebElement target = driver.findElement(By.xpath("//div[@id='droppable']"));
        // message for console - checking execution flow of program
        System.out.println("opening page...perform drag and drop");
        Actions act = new Actions(driver);
        act.dragAndDrop(source, target).build().perform();
        System.out.println("drag N drop Done...");


Instructor-led Training

Important note: we like to tell you that free tutorials are useful to get started but if you are interested in the best online LIVE Master of Automation Testing training program from the experts, please refer to the following link:

For Instructor-led training

Master of Functional Automation Testing

For Self-Driven training

Automation with Selenium WebDriver (Java)


This is all about how to work with iframes and manage the iframe. We hope you get the all-essential concepts to manage iframes. In the next lesson, we will talk about the wait and the types of wait in selenium. In the next tutorial, we will also discuss how and where to put wait conditions in the selenium script.

Until then, Happy learning 🙂

Similar Posts