Working with Frames in Selenium Webdriver

What is iFrame? An iFrame (Inline Frame) is an HTML document embedded inside the current HTML document on a website. iFrame HTML element is used to insert content from another source, such as an advertisement, into a Web page. A Web designer can change an iFrame's content without making them reload the complete website. A website can have multiple frames on a single page. And a frame can also have inner frames (Frame in side a Frame)

 ---------------------------------------
|              |                             |
|              |                             |
| Frame 1 |                             |
|              |                             |
|              |                             |
|--------------|                             |
|              |          Frame 3       |
|              |                             |
|              |                             |
|              |                             |
| Frame 2 |                             |
|              |                             |
|              |                             |
|              |                             |
|              |                             |
 --------------------------------------------

In Selenium to work with iFrames, we have different ways to handle frame depending on the need. Please look at the below ways of handling frames

driver.switchTo().frame(int arg0);

Select a frame by its (zero-based) index. That is, if a page has multiple frames (more than 1), the first frame would be at index "0", the second at index "1" and so on.
Once the frame is selected or navigated , all subsequent calls on the WebDriver interface are made to that frame. i.e the driver focus will be now on the frame. What ever operations we try to perform on pages will not work and throws element not found as we navigated / switched to Frame.

Parameters: Index - (zero-based) index
Returns: driver focused on the given frame (current frame)
Throws: NoSuchFrameException - If the frame is not found.

Example: if iframe id=webklipper-publisher-widget-container-frame, it can be written as driver.switchTo().frame("webklipper-publisher-widget-container-frame"); Below is the code snippet to work with switchToFrame using frame id.

public void switchToFrame(int frame) {
		try {
			driver.switchTo().frame(frame);
			System.out.println("Navigated to frame with id " + frame);
		} catch (NoSuchFrameException e) {
			System.out.println("Unable to locate frame with id " + frame
					+ e.getStackTrace());
		} catch (Exception e) {
			System.out.println("Unable to navigate to frame with id " + frame
					+ e.getStackTrace());
		}
	}

driver.switchTo().frame(String arg0);

Select a frame by its name or ID. Frames located by matching name attributes are always given precedence over those matched by ID.
Parameters: name Or Id - the name of the frame or the id of the frame element.
Returns: driver focused on the given frame (current frame)
Throws: NoSuchFrameException - If the frame is not found

Below is the example code snippet using frame name.

 
public void switchToFrame(String frame) {
		try {
			driver.switchTo().frame(frame);
			System.out.println("Navigated to frame with name " + frame);
		} catch (NoSuchFrameException e) {
			System.out.println("Unable to locate frame with id " + frame
					+ e.getStackTrace());
		} catch (Exception e) {
			System.out.println("Unable to navigate to frame with id " + frame
					+ e.getStackTrace());
		}
	}

driver.switchTo().frame(WebElement frameElement);

Select a frame using its previously located WebElement.
Parameters: frameElement - The frame element to switch to.
Returns: driver focused on the given frame (current frame).
Throws: NoSuchFrameException - If the given element is neither an iframe nor a frame element. And StaleElementReferenceException - If the WebElement has gone stale.

Below is the example code to send an Element to the and switch.

public void switchToFrame(WebElement frameElement) {
		try {
			if (isElementPresent(frameElement)) {
				driver.switchTo().frame(frameElement);
				System.out.println("Navigated to frame with element "+ frameElement);
			} else {
				System.out.println("Unable to navigate to frame with element "+ frameElement);
			}
		} catch (NoSuchFrameException e) {
			System.out.println("Unable to locate frame with element " + frameElement + e.getStackTrace());
		} catch (StaleElementReferenceException e) {
			System.out.println("Element with " + frameElement + "is not attached to the page document" + e.getStackTrace());
		} catch (Exception e) {
			System.out.println("Unable to navigate to frame with element " + frameElement + e.getStackTrace());
		}
	}

Some times when there are multiple Frames (Frame in side a frame), we need to first switch to the parent frame and then we need to switch to the child frame. below is the code snippet to work with multiple frames.

public void switchToFrame(String ParentFrame, String ChildFrame) {
		try {
			driver.switchTo().frame(ParentFrame).switchTo().frame(ChildFrame);
			System.out.println("Navigated to innerframe with id " + ChildFrame
					+ "which is present on frame with id" + ParentFrame);
		} catch (NoSuchFrameException e) {
			System.out.println("Unable to locate frame with id " + ParentFrame
					+ " or " + ChildFrame + e.getStackTrace());
		} catch (Exception e) {
			System.out.println("Unable to navigate to innerframe with id "
					+ ChildFrame + "which is present on frame with id"
					+ ParentFrame + e.getStackTrace());
		}
	}

After working with the frames, main important is to come back to the web page. if we don't switch back to the default page, driver will throw an exception. Below is the code snippet to switch back to the default content.

public void switchtoDefaultFrame() {
		try {
			driver.switchTo().defaultContent();
			System.out.println("Navigated back to webpage from frame");
		} catch (Exception e) {
			System.out
					.println("unable to navigate back to main webpage from frame"
							+ e.getStackTrace());
		}
	}

Please find the selenium google code Url Webdriver target for more information.

Selenium Tutorials: 

Comments

You should not be using sleeps in your examples -- it will encourage others who read said examples to themselves use it. It's extremely bad practice to do so. Use conditionals instead or Selenium's waits. These are far, far more appropriate.

Until you've done so, I would consider your example to be extremely unreliable.

Thank you for the feedback.. And I updated with selenium wait.until(ExpectedConditions...)

Good Content..Do you have any other examples for frames the same code is working

Good job..The description of frame is good.

Thanks a bunch for clarification :)
I have one doubt, suppose I am in child frame of nested frame, and triggered driver.switchTo().defaultContent(); so where is my current control will flow?

The best part of this tutorial is the explanation about iframe in the start of the article.

It would be great if you can share some example application which can be tested

I am trying to use "public void switchToFrame(WebElement frameElement)" mentioned above and in this method when I use isElementPresent(frame) my IDE is throwing an error to create a method isElementPresent. How do I overcome this??

It’s amazing to know that we can handle frames easily in Selenium webdriver.
Could you please help me to know will ‘https’ self certificate works? And can dynamically retrieve pass value to IFrame via parameters.

Thanks in advance.
Keep rocking!!

i m trying to get text or title of many frames together using for loop...not by single statement.so how it will work

Add new comment