How to work with Window and Frames in Selenium WebDriver

In this article we will see the methods to work with Window and Frame(IFRAME elements) in selenium web driver. I use c#(VS2010).

-To switch focus to another window by its Name.
 driver.SwitchTo().Window("The name of the window to select");
Note : If we want to switch the focus, then we use this. Mostly used when we need to change one window to another. The name is in string format for the title of the window.

-To switch focus to another frame by Zero based Index
 -To switch focus to another frame by its name or ID
-To switch focus to An OpenQA.Selenium.IWebDriver frame.( an object of IWebDrive)
Note : In hear, element is a IWebElement type object. Actually its a real web element. For the element object finding, I use
private IWebElement element; // declaring element, outside of method

And for switch frame parameter, I use this
element = driver.FindElement(By.XPath("Xpath Location"));
Note: Xpath location is one of the popular way to identify web elements accurately.

-To switch focus to currently focused element OR the body element [if no element focused]

-To Select either the first frame on the page or the main document when a page contains iFrames

-To switch to the currently active modal dialog
driver.SwitchTo().Alert(); be continued... thanks..:)


  1. how can i automate youtube embed video in a Iframe of webpage by selenium?

    1. From driver, focus to iframe and get embedded youtube link , then go to the link , you will get the video. Now.. this is default behavior of default youtube player. If you have stream coming in chunk, you need to control the player(like flash player). In that case use image based action (sikuli) or JavaScript to play the video and load.
      This is fully depends on how your site is playing youtube videos.