Handling Frames

  • HTML frames are used to divide the browser window into the multiple sections where each section can be land a separate HTML document.
  • Frames are section of the web page displayed on top window.
  • Whenever we access the page then focused is on the top window.
  • In Manual testing we can access element directly in any frame.

Selenium WebDriver Steps

package HandlingElementsInSelenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class HandlingFrames {

	public static void main(String[] args) throws InterruptedException {
		
		/*    Selenium Test Scripts
		 *    a) Launch a web page with multiple frames
		 *    b) Operate an element in 3rd frame
		 *    c) Operate an element in 1st frame
		 *    d) Operate an element in 2nd frame
		 */
	
	System.setProperty("webdriver.chrome.driver", "F:\\chromedriver.exe");
	WebDriver driver = new ChromeDriver();
	driver.manage().window().maximize();
	
	//Launch a web page with multiple frames                
	driver.get("https://seleniumhq.github.io/selenium/docs/api/java/index.html");
	
	//Switch from top to 3rd frame
	driver.switchTo().frame("classFrame");
	Thread.sleep(2000);
	
	//operate an element in 3rd frame
	driver.findElement(By.linkText("org.openqa.selenium.chrome")).click();
	
	//Back to top window
	driver.switchTo().defaultContent();
	
	// Switch from top window to 1st frame
	driver.switchTo().frame(0);
		
	//Operate an element in 1st frame
	driver.findElement(By.linkText("com.thoughtworks.selenium")).click();
	Thread.sleep(2000);
	
	//Back to top window/default window
	driver.switchTo().defaultContent();
		
	//Switch from top window to 2nd frame
	driver.switchTo().frame("packageFrame");
	Thread.sleep(4000);
	
	//Operate an element in 2nd frame
	driver.findElement(By.linkText("Selenium")).click();
	
	driver.close();
	}
}

Next

Leave a comment

Design a site like this with WordPress.com
Get started