What are Frames? How to handle frames in Selenium WebDriver with C#?

IFrame ( Inline Frame) or Frame is an HTML document that is included in another HTML document and is used to place the contents from another source. Eg: Advertisements, YouTube Videos etc…

Almost all site includes IFrame now a day, where they would be displaying advertisements or playing videos of other sources.

Now, let’s see how to identify and handling of IFrames in Selenium with C#.Net technology.

There are two ways to identify the Frames in your web application:

  • When performed right click action on the element and if you can see an option called “This Frame”. This method is shown in below screenshot:
  • Go to Page source and search for “iframe”, if u can see a tag name by IFrame meaning that the page contains iframe. This method is shown in below screenshot:

We can switch to IFrame by using:

1. Index: We can switch to IFrame by using frames “Index”. Remember that “Index” always starts with ‘0’ for frames as like arrays. Syntax: By using “Index” driver.SwitchTo().Frame(int frameIndex);

2. ID (or) Name: We can switch to IFrame by using the frame attributes “ID (or) Name.We can switch to IFrame by using the frame attributes ID (or) Name”. Syntax: By using “ID / Name” driver.SwitchTo().Frame(string frameID / Name);

3. Web Element: We can switch to IFrame by using “WebElement.” Syntax: By using “WebElement” driver.SwitchTo().Frame(IWebElement element);

How to switch back to parent window from IFrame ?? After performing the desired actions on iframe??

We can switch back to parent window by using either of the following code: //Selects either the first frame on the page or the main document when a page contains iFrames. driver.SwitchTo().DefaultContent(); //Select the parent frame of the currently selected frame. driver.SwitchTo().ParentFrame();

Identifying number of IFrame in the web page: //Finding the number of frames in page using the tagname “iframe”IList<IWebElement> frameCount = driver.FindElements(By.TagName(“iframe”)); Console.WriteLine(“IFrame count on web page: ” + frameCount.Count);

Leave a comment

Design a site like this with WordPress.com
Get started