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
driver.SwitchTo().Frame(0);
 -To switch focus to another frame by its name or ID
driver.SwitchTo().Frame("ID");
-To switch focus to An OpenQA.Selenium.IWebDriver frame.( an object of IWebDrive)
driver.SwitchTo().Frame(element);
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"));
driver.SwitchTo().Frame(element);
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]
driver.SwitchTo().ActiveElement();

-To Select either the first frame on the page or the main document when a page contains iFrames
driver.SwitchTo().DefaultContent();

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

...to be continued... thanks..:)

2 comments:

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

    ReplyDelete
    Replies
    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.

      Delete