What is a frame?

Frame is just like as a container where few elements can be grouped.

How to identify frame inside a webpage?

There are different ways to identify frame inside a webpage Way 1:

  • Open webpage in a browser.

webelement in a webpage

  • Right click on webelement in a webpage

frame inside a webpage Way 2:

  • Open webpage in a browser.
  • Right click on webelement in a webpage
  • Open source code(Html Code) of the webpage by clicking inspect element with firebug option see below image.

iframe source code

How to handle an element inside the frame?

There are 4 ways to handles frames in selenium webdriver.

Switch to frame by using index :

Method 1 : Suppose if there is single frame in a webpage then we can switch to the iframe by using index. Here is the sample code: Syntax : driver.switchTo().frame(int index); Example : driver.switchTo().frame(0); Note : By default single frame index value ‘ 0’.That means when webpage has  only one frame then the index will be zero. Method 2 : Suppose if there are 3 frames in a webpage then we can switch to the iframe by using index. Here is the sample code: Syntax : List framelist=driver.findElements(By.tagName("iframe")); driver.switchTo().frame(framelist.get(int index)); Example : List framelist=driver.findElements(By.tagName("iframe")); //switchTo 1st frame by using index driver.switchTo().frame(framelist.get(0));   //switchTo 2nd frame by using index driver.switchTo().frame(framelist.get(1)); //switchTo 3rd frame by using index driver.switchTo().frame(framelist.get(2));

Switch to frame by using Id or Name :

We can also use Name and Id attributes of iframe  through which we can switch to iframes. Here is the sample code: Syntax 1: driver.switchTo().frame(“Id of  the element”); Html code : iframe html code                                                                                   Example : driver.switchTo().frame(“rightMenu”); Syntax 2: driver.switchTo().frame(“Name of  the element”); Html code : iframe html code                                                                               Example : driver.switchTo().frame(“rightMenu”);

Switch to frame by using WebElement :

We can also switch to the frame using webelement. Here is the sample code: Syntax : driver.switchTo().frame(WebElement); Html code : iframe html code Example : driver.switchTo().frame(driver.findElement(By.name(“rightMenu”)));

How to get all the frames in a webpage ?

get all the frames in a webpage Html code : Open notepad and type the below Html code and save as frames.html

 

Click on Gmail

  Example : package seleniumProject; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class getTotalFrames { public static void main(String[] args) { //   Open Firefox Browser          WebDriver driver = new FirefoxDriver();  //   Open AppURL In Browser driver.get("file:///C:/Users/Hanumanthu/Downloads/frames.html");  // count all the frames on a webpage List total_frames=driver.findElements(By.tagName("iframe")); System.out.println(total_frames.size()); //close the current browser window driver.close(); } }

How to verify TestNg text inside 3rd frame?

verify TestNg text inside 3rd frame Selenium Sample Code : package seleniumProject; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class getTotalFrames { public static void main(String[] args) {    // Open Firefox Browser   WebDriver driver = new FirefoxDriver(); //  Open AppURL In Browser driver.get("file:///C:/Users/Hanumanthu/Downloads/frames.html");  //count all the frames on a webpage List total_frames=driver.findElements(By.tagName("iframe")); System.out.println(total_frames.size());  //Switch to 3rd Frame by using Index driver.switchTo().frame(total_frames.get(2));   //Identify and get the text and store into testNg_text variable String testNg_text=driver.findElement(By.xpath("html/body/h2[1]")).getText();    //print the text from 3rd frame System.out.println(testNg_text);   //verify the text from 3rd frame if(testNg_text.equals("TestNG")) { System.out.println("TestNG text verified successfully"); }else { System.out.println("TestNG text not verified successfully"); }  //How switch back to main window from inside any frame ? driver.switchTo().defaultContent(); //Close the Firefox Browser driver.close(); } } Note : switch back to main window from inside any frame

1. Switch to 3rd Frame by using Index

Syntax :   driver.switchTo().frame(total_frames.get(index)); Example :   driver.switchTo().frame(total_frames.get(2));

2.Switch to 3rd  Frame by using Id

Syntax :   driver.switchTo().frame(“Id of the element”); Example :   driver.switchTo().frame(“selenium”); If no id then go for Name.

 3.Switch to 3rd  Frame by using Name

Syntax :   driver.switchTo().frame(“Name of the element”); Example :   driver.switchTo().frame(“selenium”);

4.Switch to 3rd  Frame by using WebElement

Syntax :   driver.switchTo().frame(WebElement); Example :   driver.switchTo().frame(driver.findElement(By.name(“selenium”);

 How switch back to main window from inside any frame ?

Syntax : driver.switchTo().defaultContent();

 How to handle frame in selenium webdriver using java

TestSteps: Open Firefox Browser Open AppURL In Browser Get the Title of WebPage Verify Title of WebPage Enter the username Enter the password Clicking On Login Button Identify and get  the Welcome selenium text Verify  Welcome selenium text Switch to frame Handle DropDown in Selenium

  1.  How to print all the dropdown values
  2. How to Select the dropdown value inside a frame
  3. verify selected value from dropdown
  4. How to Verify dropdown values

Again switch back to main window from frame Clicking On Logout Button Close the Firefox Browser Selenium Code : package com.tests; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; importorg.openqa.selenium.support.ui.Select; public class OrangeHRM_login { public static void main(String[] args) throws Interrupted Exception { // open the firefox browser WebDriver driver = newFirefoxDriver(); // navigate the AppUrl diver.get("http://127.0.0.1/orangehrm-2.5.0.2/login.php"); //Get the Title of WebPage String title=driver.getTitle(); //print the title of the webpage System.out.println(title); //Verify Title of the WebPage if (title.equals("OrangeHRM - New Level of HR Management")) { System.out.println("title is verified successfully"); } else { System.out.println("title is not verified successfully"); } // Enter the username   driver.findElement(By.name("txtUserName")).sendKeys("selenium"); // Enter the password driver.findElement(By.name("txtPassword")).sendKeys("selenium"); // Clicking On Login Button driver.findElement(By.name("Submit")).click(); // Identify and get  the Welcome selenium text and store into text variable String text = driver.findElement(By.xpath("//*[@id='option-menu']/li[1]")).getText(); // Print the welcome selenium text System.out.println(text); //  To verify whether the welcome page successfully opened or not if (text.equals("Welcome selenium")) { System.out.println("Welcome selenium is verified successfully"); } else { System.out.println("Welcome selenium is not verified successfully"); } // Switch to Frame by ID driver.switchTo().frame("rightMenu"); System.out.println("****How to print all the dropdown values inside a frame****"); Identify the dropdown // Identify the dropdown WebElement dropdown = driver.findElement(By.id("loc_code")); // Identify  dropdown values  and store into droplist  variable List droplist = dropdown.findElements(By.tagName("option")); // How to print all the dropdown values for (inti = 0; i<droplist.size(); i++) { System.out.println(droplist.get(i).getText()); } System.out.println("*****How to Select the dropdown value inside a frame*****"); Select the dropdown value inside a frame /*                     If u want to select the dropdown value then we need to create the select object for that dropdown                     */ Select s = new Select(dropdown); // Select the dropdown value by using index s.selectByIndex(2); //s.selectByVisibleText("Emp. First Name"); //s.selectByValue("1"); System.out.println("****verify selected value from dropdown inside a frame *****"); verify selected value from dropdown inside a frame //get the selected value and store into selected_value variable String  selected_value=s.getFirstSelectedOption().getText(); //print selected value System.out.println("selected_value  :"+selected_value); //verify selected value from dropdown if (selected_value.equals("Emp. First Name")) { System.out.println("selected value  verified successfully"); } else { System.out.println("selected value not verified successfully"); } // How switch back to main window from inside any frame driver.switchTo().defaultContent(); // Clicking On Logout Button driver.findElement(By.xpath("//*[@id='option-menu']/li[3]/a")).click(); // Close the Firefox Browser driver.close(); } } Click here: How to verify multiple checkboxes in a webpage using selenium? Click here: Steps to configure Selenium WebDriver with java to develop test scripts

Selenium Training in Hyderabad

Kosmik Technologies is on of the best selenium training institutes in Hyderabad. We are providing lab facilities with complete real-time training. Training is based on complete advance concepts. So that you can get easily "hands-on experience". We will give 100% job assistance.