What is Frame ?

The Frame/iFrame is nothing but another webelement in the HTML page, which displays another part of the webpage. We can handle frames/iframes present on the webpage using the browser.switchTo() command in protractor.

If you look at the DOM structure of a page that contains an iframe, you will never find the inner content of the iframe. And you won't be able to interact with it via the DOM. We have to switch into the frame to see the elements present in the frame.

Below frame page is present at : https://chercher.tech/practice/frames-example-selenium-webdriver frames-example-selenium-webdriver

Difference between Frames and iFrames

Both Frame and iFrame are treated similar manner with the protractor; Protractor does not differentiate them so that we can handle both of them in the same way.

The Frame is an HTML tag and used to divide the same web page or same domain into various frames/windows. Used as <frame> tag, it specifies each frame within a frameset tag. Frames are used along with frameset; frameset contains the multiple frames.

<frameset rows = "10%,80%,10%">
	<frame id= "first" name = "top" src = "/html/top_frame.htm" />
	<frame name = "main" src = "/html/main_frame.htm" />
	<frame name = "side" src = "/html/side_frame.htm" />
</frameset>


Iframe as <iframe> is also a tag used in HTML, but it specifies an inline frame, which means it is used to embed not only the same domain but also other webpages within the current HTML document. It is like a Television; it displays a thing which is present somewhere else.

<iframe id="ifr" name="demo" src="demo.html" height="200" width="300"></iframe>
  • Frame requires a frameset, but iframes donot requires framesets.
  • Placement of Frames is a bit difficult compared with iFrames
  • Adjusting the size and width of the Frame is difficult when compared with iFrame.
  • Frames cannot contain nested frames (frame inside the frame), but iFrames can contain nested iFrames.
  • HTML5 does not support Frames but supports iFrames.

Developers try to avoid using Iframes as these are external documents (webpages), so there is a chance of Phishing.

Window handling in Protractor

Presence of Frame in protractor

It is essential for the tester to check whether an element is inside an iframe/frame or just on a webpage. If the element is inside a frame, then we have to switch into the frame to access the element. 1. Right-click on the page (not on the element) which part you want to check.

2. On the right-click options, you can find This Frame Option; if this option is present, then there is an iframe else there is no iframe frame-selenium-webdriver

3. Webpage without a frame. noframe-selenium-webdriver

Ways to select the dropdown option

How to find iframe/frame in protractor

We can find the iframe/frame using right-click in manual testing, finding the iframe/frame in protractor is little tricky one.

Below are the ways to find the iframe/frame:

  • Using Element (widely used)
  • Using Index


Using Element : Most of the time, there will be multiple iframe/frame, and few of them may share the same id and name, so in such scenarios, we cannot use id or name for finding the frame. To find the iframe/frame uniquely, we have to find it as an element, the way we follow for normal elements.

Here we can use locators ( except id, name, as we used already) like classname, XPath CSS. We cannot use link text and partial link text as these two are only applicable for anchor tag <a>.

We also should refrain using a tagname locator as there are multiple frames, so we may not get the unique element.

Lets consider the below iframes, and xpath for this iframe would be :

  • Xpath for 1st iFrame : //iframe[@src='demo.html']
  • Xpath for 2nd iFrame : //iframe[@class='second']
<iframe id="ifr" name="demo" src="demo.html" height="200" width="300"></iframe>
<iframe id="ifr" name="demo" class='second' src="width.html" height="200" width="300"></iframe>
<iframe id="ifr" name="demo" src="width.html" height="200" width="300"></iframe>

// switch to 1st frame
browser.switchTo().frame(element(by.xpath("//iframe[@src='demo.html']")).getWebElement())


Using Index : protractor assigns an index to every frame present in the page, using index is the least preferred way of finding the frame as in future frame position may change when development introduce another frame in between

<iframe id="ifr" name="demo" src="demo.html" height="200" width="300"></iframe>
<iframe id="ifr" name="demo" class='second' src="width.html" height="200" width="300"></iframe>
<iframe id="ifr" name="demo" src="width.html" height="200" width="300"></iframe>

// switch to 1st frame
browser.switchTo().frame(1)

click operation using Mouse actions

Video Tutorial for Frames in protractor

Subscribe to my youtube channel :

Handle Hidden division Pop Up / Calendar Pop up using Protractor

Handle single iFrame in protractor

If we want to access any element inside an iframe in protractor, we must find and switch to that iframe and access the element; protractor throws NoSuchElementFound Exception unless we switch to the iframe.

single-frame-example-selenium-webdriver

Scenario : Fetch the value from the Topic (page level) and write it to topic text bar present in iframe 1

Page Url : https://chercher.tech/practice/frames-example-selenium-webdriver
single-frame-source-selenium-webdriver

Steps to solve the scenario : 1. Open the browser and navigate to : https://chercher.tech/practice/frames-example-selenium-webdriver

// open webpage
browser.get("https://chercher.tech/practice/frames-example-selenium-webdriver");


2. Get the text from the Topic element and store it in a string.

// resolve promise to get text value
element(by.xpath("//label/span")).getText().then(function(textValue){
	//todo
})


3. Switch to the Frame1, frame has attribute id='frame1' and which is unique, so we can use id way switching to the frame.

// switch to frame1
browser.switchTo().frame(element(by.id("frame1")).getWebElement());


4. Find the Text bar in the frame1 and enter the stored text.

// set the value of the textbar to the value stored
element(by.xpath("//input[@type='text']")).sendKeys(textValue);


Complete program for single frame handling

import { browser, element, by, ExpectedConditions, protractor} from 'protractor'
import { Alert } from 'selenium-webdriver';
describe('Protractor Typescript Demo', function() {
	browser.ignoreSynchronization = true; // for non-angular websites
	it('iFrame operation', function() {
		// browser.manage().timeouts().implicitlyWait(10000)
		browser.get("https://chercher.tech/practice/frames-example-selenium-webdriver");
		// resolve the promise
		element(by.xpath("//label/span")).getAttribute("innerText").then(function(textValue){
			// switch to frame1
			browser.switchTo().frame(element(by.id("frame1")).getWebElement());
			// set the value of the textbar to the value stored
			element(by.xpath("//input[@type='text']")).sendKeys(textValue);
		})
	});
});

Waits in Protractor

Handling nested iFrame in protractor

Sometimes we will have multiple and nested iframes on a webpage if we have nested iframes, we have to switch to iframe inside a frame.

We can access only the content of the frame when we are inside a frame, and we cannot access outside the frame or inside any other frame.

Scenario : Check the checkbox present in the iframe 3, iframe 3 is present inside iframe 1.

Page Url : https://chercher.tech/practice/frames-example-selenium-webdriver
nested-frame-selenium-webdriver

The Solution to the scenario : Ignoring routine steps (open browser, page, wait)
1. Find the iframe1 and store it as webelement.

//find the frame1 and store it in webelement
let frame1:WebElement = element(by.id("frame1")).getWebElement();


2. Switch to iframe 1 using switchTo().frame() command

// switch to iframe1
browser.switchTo().frame(frame1);


3. Find the iframe 3 and switch to it.

// find the frame 3
let frame3:WebElement = element(by.xpath("//iframe[@id='frame3']")).getWebElement();
// switch to frame 3
browser.switchTo().frame(frame3);


4. Find the Checkbox and click it if it is not already checked.

// find the checkbox
let checkbox:WebElement = element(by.xpath("//input[@type='checkbox']"));
checkbox.isSelected().then(function(checked){
	// if check box is not selected then click the checkbox
	if(! checked){
		checkbox.click();
	}
})


Complete program to handle nested text bar.

import { browser, element, by, ExpectedConditions, protractor} from 'protractor'
import { Alert, WebElement } from 'selenium-webdriver';
describe('Protractor Typescript Demo', function() {
	browser.ignoreSynchronization = true; // for non-angular websites
	it('iFrame operation', function() {
		browser.manage().timeouts().implicitlyWait(10000)
		// open webpage
		browser.get("https://chercher.tech/practice/frames-example-selenium-webdriver");
		//find the frame1 and store it in webelement
		let frame1:WebElement = element(by.id("frame1")).getWebElement();
		// switch to frame1
		browser.switchTo().frame(frame1);
		// find the frame 3
		let frame3:WebElement = element(by.xpath("//iframe[@id='frame3']")).getWebElement();
		// switch to frame 3
		browser.switchTo().frame(frame3);
		// find the checkbox
		let checkbox:WebElement  = element(by.xpath("//input[@type='checkbox']"));
		// if check box is not selected then click the checkbox
		checkbox.isSelected().then(function(checked){
			// if check box is not selected then click the checkbox
			if(! checked){
				checkbox.click();
			}
		})
	});
});

Jasmine with protractor

Switch to Parent Frame : switchToParentFrame() method in protractor switches the control to outer position(one place) in the webpage, the outer position could be a frame or page level.

In simple terms, switchToParentFrame() method exits the current frame.

After performing a particular task, we have to move out of the frame; otherwise, we cannot access the elements outside the frame.

protractor provides the browser.switchToParentFrame() method to move out of the current frame, once we move out of the frame, we can access the elements outside that frame, but we cannot access the elements inside that frame

Scenario :

  • 1. Check the checkbox present in the iframe 3; iframe 3 is present inside iframe 1
  • 2. After checking the checkbox, move back to iframe 1 and enter "selenium" text in the topic text bar
  • 3. Now move back to page level and compare the topic(header) text is not equal to "selenium webdriver."

Page Url :https://chercher.tech/practice/frames-example-selenium-webdriver
parent-frame-exampe-selenium-webdriver


The Solution to Scenario : skipping a few steps that were covered in the previous scenario.

1. Switch to iframe 1 and switch to iframe 3, check the checkbox

2. Now we have to navigate back to frame 1 by using the browser.switchToParentFrame() method

// navigate to parent frame, which is iframe 1
browser.switchToParentFrame();

3. Find the text bar and enter the "selenium" text" in it.

// set the value of the textbar to the value stored
element(by.xpath("//input[@type='text']")).sendKeys("selenium");


4. Now we have to navigate back to the page by using the browser.switchToParentFrame() method

// navigate to parent, which is page
browser.switchToParentFrame();


5. Find the topic header element and fetch the text.

// store the text value
element(By.xpath("//label/span")).getText().then(function(textValue){
	//todo
});


6. Compare the topic text with expected value "selenium webdriver"

if(textValue === "selenium webdriver"){
	System.out.println("Topic value is equal to 'selenium webdriver'");
}


Complete program to use parent frame method

import { browser, element, by, ExpectedConditions, protractor} from 'protractor'
import { Alert, WebElement } from 'selenium-webdriver';
describe('Protractor Typescript Demo', function() {
	browser.ignoreSynchronization = true; // for non-angular websites
	it('iFrame operation', function() {
		browser.manage().timeouts().implicitlyWait(10000)
		// open webpage
		browser.get("https://chercher.tech/practice/frames-example-selenium-webdriver");
		//find the frame1 and store it in webelement
		let frame1:WebElement = element(by.id("frame1")).getWebElement();
		// switch to frame1
		browser.switchTo().frame(frame1);
		// find the frame 3
		let frame3:WebElement = element(by.xpath("//iframe[@id='frame3']")).getWebElement();
		// switch to frame 3
		browser.switchTo().frame(frame3);
		// find the checkbox
		let checkbox:WebElement  = element(by.xpath("//input[@type='checkbox']"));
		// if check box is not selected then click the checkbox
		checkbox.isSelected().then(function(checked){
			// if check box is not selected then click the checkbox
			if(! checked){
				checkbox.click();
			}
		})
		// navigate to parent frame, which is frame 1
		browser.switchToParentFrame();
		// set the value of the textbar to the value stored
		browser.findElement(by.xpath("//input[@type='text']")).sendKeys("selenium");
		// navigate to parent, which is page
		browser.switchToParentFrame();
		// store the text value
		element(by.xpath("//label/span")).getText().then(function(textValue){
			//verify the value matches or not
			if(textValue === "selenium webdriver"){
				console.log("Topic value is equal to 'selenium webdriver'");
			}
		});
	});
});

Jenkins with Protractor

Default Content in protractor

Default Content : switchTo().defaultContent() method exits all the iframes and the places the protractor control at the page level where as switchToParentFrame() method exits the current iframe.

Once we reach the page level, we cannot access any elements inside the iframe unless we switch to it.

frames-example-selenium-webdriver

Scenario : Check the check box(frame 3), and select the 'Avatar' option from the Animals dropdown(frame 2).

Page Url : https://chercher.tech/practice/frames-example-selenium-webdriver default-content-frames-selenium-webdriver

The Solution to the scenario :
1. switch to frame 1 and switch to frame 3
2. Check the checkbox
3. Now exit from all the frames using switchTo().defaultContent()

// navigate to page level
browser.switchTo().defaultContent();


4. Switch to iframe 2

//switch to frame2
browser.switchTo().frame(frame2);


5. Find the Animals dropdown and store it in the WebElement type variable.

//find the dropdown
element(by.tagName("select"));


6. Handle the dropdown using clicks.

//click the 'avatar' option
element(by.xpath("//option[text()='Avatar']")).click()


Complete program for default content in protractor

import { browser, element, by, ExpectedConditions, protractor} from 'protractor'
import { Alert, WebElement } from 'selenium-webdriver';
describe('Protractor Typescript Demo', function() {
	browser.ignoreSynchronization = true; // for non-angular websites
	it('iFrame operation', function() {
		browser.manage().timeouts().implicitlyWait(10000)
		// open webpage
		browser.get("https://chercher.tech/practice/frames-example-selenium-webdriver");
		//find the frame1 and store it in webelement
		let frame1:WebElement = element(by.id("frame1")).getWebElement();
		// switch to frame1
		browser.switchTo().frame(frame1);
		// find the frame 3
		let frame3:WebElement = element(by.xpath("//iframe[@id='frame3']")).getWebElement();
		// switch to frame 3
		browser.switchTo().frame(frame3);
		// find the checkbox
		let checkbox:WebElement  = element(by.xpath("//input[@type='checkbox']"));
		// if check box is not selected then click the checkbox
		checkbox.isSelected().then(function(checked){
			// if check box is not selected then click the checkbox
			if(! checked){
				checkbox.click();
			}
		})
		// navigate to page level
		browser.switchTo().defaultContent();
		//switch to frame2
		browser.switchTo().frame("frame2");
		//find the dropdown
		element(by.tagName("select")).click()

		//click the 'avatar' option
		element(by.xpath("//option[text()='Avatar']")).click()
	}
}



Errors in Protractor

We can write the above program in the switch to parent Frame way as well

import { browser, element, by, ExpectedConditions, protractor} from 'protractor'
import { Alert, WebElement } from 'selenium-webdriver';
describe('Protractor Typescript Demo', function() {
	browser.ignoreSynchronization = true; // for non-angular websites
	it('iFrame operation', function() {
		browser.manage().timeouts().implicitlyWait(10000)
		// open webpage
		browser.get("https://chercher.tech/practice/frames-example-selenium-webdriver");
		//find the frame1 and store it in webelement
		let frame1:WebElement = element(by.id("frame1")).getWebElement();
		// switch to frame1
		browser.switchTo().frame(frame1);
		// find the frame 3
		let frame3:WebElement = element(by.xpath("//iframe[@id='frame3']")).getWebElement();
		// switch to frame 3
		browser.switchTo().frame(frame3);
		// find the checkbox
		let checkbox:WebElement  = element(by.xpath("//input[@type='checkbox']"));
		// if check box is not selected then click the checkbox
		checkbox.isSelected().then(function(checked){
			// if check box is not selected then click the checkbox
			if(! checked){
				checkbox.click();
			}
		})
		// navigate to parent frame, which is frame 1
		browser.switchToParentFrame();
		// set the value of the textbar to the value stored
		element(by.xpath("//input[@type='text']")).sendKeys("selenium");
		// navigate to parent, which is page
		browser.switchToParentFrame();
		// store the text value
		element(by.xpath("//label/span")).getText().then(function(textValue){
			//verify the value matches or not
			if(textValue === "selenium webdriver"){
				console.log("Topic value is equal to 'selenium webdriver'");
			}
		});
		//switch to frame2
		browser.switchTo().frame(element(by.id("frame2")).getWebElement());

		//find the dropdown
		element(by.tagName("select")).click()

		//click the 'avatar' option
		element(by.xpath("//option[text()='Avatar']")).click()
	}
}

Protractor Installation

Possible Routes of iFrame Navigation

The below image explains the possible routes of the iFrame navigation. possible-routes-selenium-webdriver

Protractor Interview Questions

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
  • Sanjay P
    Super helpful content you have in here. Very neat and with clear examples. Keep going...
    Reply