Test Diary


Social Profile

Recent Comments

My Sixth Selenium WebDriver Test (Frames)

Olufemi Ade-OlusileOlufemi Ade-Olusile

Menu Next Topic

In this post we will be looking at how Selenium handles frames. A lot of webpages today, still use frames and frame sets, even though frame sets have been removed from the HTML 5 standard. Therefore, it is still necessary to understand how Selenium handles frames.

One of the easiest ways to handle frames with Selenium is by using the switchTo() method. This method tells our driver to switch to a window or a frame or other methods that depend on it. In our case, we will be using the frame() method with the switchTo method.

Next, when we chain the frame() method to the switchTo() method, we need to parse a parameter into the frame() method to point Selenium on what frame it should switch to. There are different parameters that can be parsed, but this depends on the HTML of the frame you are pointing Selenium to. For example, a frame may have any or all of the following; index, webElement or String.

In our sample test page, we have a frame with the following HTML.

<iframe width="500" height="300"

name="testframe" src="http://www.testdiary.com/frametestpage/">


Based on this HTML, our frame has a name webElement (testframe), which can be parsed into the frame() method to locate this frame. For example frame(“testframe”);

Now let’s follow the steps below and then dive into our code to understand how we test/implement a frame.

Steps to follow

  1. Navigate to the sample test page.
  2. Switch to the frame in the test page and capture the text in the frame
  3. Verify that you have the correct text from the frame
  4. Switch back to the Parent frame (Test page).
Sixth Test (Frames) 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class TestdiaryFrames {
public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();


String contactUrl = "http://www.testdiary.com/training/selenium/selenium-test-page/";


//wait until Selenium can find the frame with locator name "testframe" on the test page.
(new WebDriverWait(driver, 10))

// Declaring a local variable of type "WebElement" to initialize the
// value of the webElement (testframe)
WebElement testframe = driver.findElement(By.name("testframe"));

//using the switchTo method you can parse "testframe" webElement as a parameter
// into the frame method

//get the text displayed in the frame, as selenium is now using the Iframe it was pointed too
String expectedFrameText =driver.findElement(By.id("testpagelink")).getText();
String actualFrameText = "My frame text";

// verify the texts
System.out.println("Found my frame text");
else {
System.out.println("Did not find my frame text");

// This will return driver back to the original Parent Frame
// Which is the selenium-test-page


In conclusion, we have been able to switch to the test frame on the sample test page and verify the text in it. You can use this same approach to point selenium to any web frame and test what’s in it’s body or container. You can download or clone this test from the Testdiary Github.

Click: Frames Happy Testing!!!

Menu Next Topic

“My name is Olufemi Ade-Olusile, and I am a Software Developer in Test. I am the owner of "Test diary" which is aimed at inspiring and teaching software testers.