In my previous article, IFrame execution, using Selenium Webdriver - Part One, I have explained the usage/advantages of frame and the execution of iframe, using Selenium Webdriver, I have covered one scenario of iframe with Selenium. Before reading this article, I suggest you refer to or read my previous article, IFrame Execution Using Selenium WebDriver - Part One
In this article, I will cover two different scenarios with an iframe inside another iframe, which are nested iframes.
Scenario 2

From the image given above, we have taken two different frames, which are within an iframe1. We have used another iframe i.e. iframe2. Whenever we want to automate normal controls without any containers like div or an iframe on a Webpage, it will be easy to automate just by using their control properties. Whenever we have controls within an iframe and this iframe is available in another iframe, it will be difficult to identify & automate an iframe and its controls.
In this scenario, we will identify the first frame i.e iframe1 by using its properties like Id="Frame1" or name="Frame1". Once the frame1 is identified, we will switch into the frame1. Subsequently, we will identify each control with the properties like
Scenario 2

From the image given above, we have taken two different frames, which are within an iframe1. We have used another iframe i.e. iframe2. Whenever we want to automate normal controls without any containers like div or an iframe on a Webpage, it will be easy to automate just by using their control properties. Whenever we have controls within an iframe and this iframe is available in another iframe, it will be difficult to identify & automate an iframe and its controls.
In this scenario, we will identify the first frame i.e iframe1 by using its properties like Id="Frame1" or name="Frame1". Once the frame1 is identified, we will switch into the frame1. Subsequently, we will identify each control with the properties like
- For FirstName field of iframe1, we have control properties like id="Fname1" or name="Fname1"
- For LastName field of iframe1, we have control properties like id="Lname1" or name="Lname1"
- For Password field of iframe1, we have control properties like id="Password1" or name="Password1"
- For Email field of iframe1, we have control properties like id="Email1" or name="Email1"
- For Login button of iframe1, we have control properties like id="Login1" or name="Login1"
Once these controls are identified with their control properties, then we can automate each of them, using Selenium. Once all the controls of iframe1 are automated, we will identify the iframe2 with their control properties like id="Frame2" or name="Frame2". Once iframe2 is identified, we will switch into iframe2 and perform automation on the controls, which are available in iframe2.
Example
Example
- For FirstName field of iframe2, we have control properties like id="Fname2" or name="Fname2"
- For LastName field of iframe2, we have control properties like id="Lname2" or name="Lname2"
- For Password field of iframe2, we have control properties like id="Password2" or name="Password2"
- For Email field of iframe2, we have control properties like id="Email2" or name="Email2"
- For Login button of iframe2, we have control properties like id="Login2" or name="Login2"
Once these controls are identified with their control properties, then we can automate each of them, using Selenium Webdriver. Once all the controls of iframe2 are automated, we will switch out of iframe2 and iframe1, so that we can automate the other controls, which are available outside the frames i.e iframe1 & iframe2.
- To automating outer Textbox1 and outer Textbox2, we can identify with its control properties like
- To Outer Textbox1 we have id="outerTxt1" and name="outerTxt1"
Similarly for Outer Textbox2, we have id="outerTxt2" and name="outerTxt2".
HTML for the diagram given above is given below.
HTML for the diagram given above is given below.
- <html>
- <head>
- <title>IFRAME EXAMPLE 2</title>
- </head>
- <body>
- <iframe src="D:/IFrameExamples/FirstFrame1.html" height="300" id="Frame1" name="Frame1" width="300" scrolling="no" frameborder="0" style=border:solid;color:red " >
- </iframe><br><br>
- //From the above iframe we are calling src of FirstFrame1 , all the controls available in the FirstFrame1.html will be binding to iframe1.
- Outer TextBox1 :<input type="text " id="outerTxt1 " name="outerTxt1 "><br><br>
- Outer TextBox2 :<input type="text " id="outerTxt2 " name="outerTxt2 ">
- //Here OuterTextbox1 and OuterTextbox2 will be available out of frame1 and frame2
- </body>
- </html>
FirstFrame1.html
- <html>
- <head>
- <title>FirstFrame1.html</title>
- </head>
- <body> First Name : <input type='"text" id="Fname1" name="Fname1"><br><br>
- Last Name : <input type=' "text" id="Lname1" name="Lname1"><br><br> Email : <input type='"text" id="Email1" name="Email1"><br><br>
- Password : <input type=' "text" id="Password1" name="Password1"><br><br> <input type='"text" id="Login1" name="Login1" value="Login"><br><br>
- <iframe src="D:/IFrameExamples/SecondFrame2.Html" id="Frame2" name="Frame2" height ="100" width="100" scrolling="no" frameborder="0" style=border:solid;color:red;>
- </iframe>
- //From the above frame that is Frame2 we are calling src of SecondFrame2.html , the controls available in SecondFrame2.html will be binding
- inside the iframe2.
- </body>
- </html>
SecondFrame2.Html
- <html>
- <head>
- <title>SecondFrame2.Html</title>
- </head>
- <body> First Name : <input type='"text" id="Fname2" name="Fname2"><br><br>
- Last Name : <input type=' "text" id="Lname2" name="Lname2"><br><br> Email : <input type='"text" id="Email2" name="Email2"><br><br>
- Password : <input type=' "text" id="Password2" name="Password2"><br><br> <input type='"text" id="Login2" name="Login2" value="Login"><br><br>
- </body>
- </html>
The code for the execution of IFrame, using Selenium Webdriver is given below.
- Using System;
- Using Mbunit.Framework;
- Uing Microsoft.VisualStudio.TestTools.UnitTesting;
- Using System.Threading;
- Using OpenQA.Selenium;
- Using OpenQA.Selenium.Chrome;
- Namespace SecondScenarioTesting {
- [TestClass]
- Public class SecondScenarioTesting {
- [TestMethod] // [Testclass] & [TestMethod] is used to perform unit testing of selenium code.
- Public void ScenarioSecondTests() {
- Iwebdriver driver = new ChromeDriver("D:/ChromeDriver"); // Here we need to pass the chrome driver path which is available in your local machine.
- //driver variable holds the information related to the browser.
- driver.Navigate().GoToUrl("D:/IframeExamples/IFrameExample2"); // Here we need to pass the Url which we want to test the application.
- //The above code will open the Url in chrome browser.
- driver.Manage().Window.Maximize(); // By default the browser will not be fully maximized , we need to maximize it by using this code.
- IwebElement FrameElement = driver.FindElement(By.Name("Frame1")); // Inorder to identify the first frame ie. iframe1 we can identify it by using its name property.
- // Once frame is identified , the 'FrameElement' holds all the information related to iframe1.
- driver.SwitchTo().Frame(FrameElement); // we can switch into iframe1 by passing FrameElement to Frame() method.
- IWebElement Fname = driver.FindElement(By.Id("Fname1")); // To identify first control within an iframe1 that is FirstName , we can identify it using either by id or name property.
- //'Fname ' hold all the information related to FirstName textbox field.
- Fname.SendKeys("Khaja"); // Using this line of code we can enter the given value to the textbox.
- Thread.Sleep(2000); // Used to wait for the specified time.
- IWebElement Lname = driver.FindElement(By.Id("Lname1")); // To identify lastname of iframe1 , we can identify it using its id or name property.
- //Lname holds information related LastName textbox.
- Lname.SendKeys("Moizuddin"); // This is used for entering the given value to lastname textbox.
- Thread.Sleep(2000); // wait time.
- IWebElement Email = driver.FindElement(By.Name("Email1")); // To identify email of iframe1 , we can identify it using id or name property of email field.
- Email.SendKeys("[email protected]"); // It is used to enter the given value into email textbox.
- Thread.Sleep(2000); // wait for given time.
- IWebElement Password = driver.FindElement(By.Name("Password1")); // Password can be identified using id or name property.
- Password.SendKeys("Password12345"); // It is used to enter the given value into email textbox.
- Thread.Sleep(2000); // wait for specified time.
- IWebElement LoginClick = driver.FindElement(By.Id("Login1")); // Login field we can identify by using id or name property.
- LoginClick.Click(); // Click() method is used for clicking the given button. that is Login.
- IwebElement SFrameElement = driver.FindElement(By.Name("Frame2")); // It is used for identifying iframe2 by using its properties like id or name.
- driver.SwitchTo().Frame(SFrameElement); //Once iframe2 is identified we can switch into iframe2 by passing SFrameElement to Frame() method.
- IWebElement SFname = driver.FindElement(By.Id("Fname2")); // FirstName textbox of iframe2 can be identified using id or name property.
- 'SFname'
- holds all the information related to FirstName textbox of iframe2.
- SFname.SendKeys("Khaja"); // It is used for entering the given value to FirstName textbox.
- Thread.Sleep(2000); //wait for specified time.
- IWebElement SLname = driver.FindElement(By.Id("Lname2")); // FirstName textbox of iframe2 can be identified using id or name property.
- 'SLname'
- holds all the information related to LastName textbox of iframe2.
- SLname.SendKeys("Moizuddin"); // It is used for entering the given value to LastName textbox.
- Thread.Sleep(2000);
- IWebElement SEmail = driver.FindElement(By.Name("Email2")); // Email textbox of iframe2 can be identified using id or name property.
- SEmail.SendKeys("[email protected]"); // It is used for entering the given value to LastName textbox.
- Thread.Sleep(2000);
- IWebElement SPassword = driver.FindElement(By.Name("Password2")); //Password textbox of iframe2 can be identified using id or name property.
- SPassword.SendKeys("Password12345"); // It is used for entering the given value to LastName textbox.
- Thread.Sleep(2000);
- IWebElement SLoginClick = driver.FindElement(By.Id("Login2")); //Login textbox of iframe2 can be identified using id or name property.
- SLoginClick.Click(); // It is used to click the login button.
- driver.SwitchTo().DefaultContent(); // It is used to come out of frame2
- driver.SwitchTo().DefaultContent(); //It is used to come out of iframe1.
- // Once focus is on the main webpage we can execute the below textboxes ie. OuterTextbox1 , OuterTextbox2
- IWebElement outerTextbox1 = driver.FindElement(By.Name("outerTxt1")); // OuterTextbox1 can be identified using id or name property.
- outerTextbox1.SendKeys("[email protected]"); // It is used for entering the given value to OuterTextbox1 field.
- Thread.Sleep(2000);
- IWebElement outerTextbox2 = driver.FindElement(By.Name("outerTxt2")); // OuterTextbox2 can be identified using id or name property.
- outerTextbox2.SendKeys("Password12345"); // It is used for entering the given value to OuterTextbox2 field.
- Thread.Sleep(2000);


Join the conversation! Your thoughts help the community grow.