sampleFrame.java

 
public class sampleFrame {
  public static void main(String[] args){
   
   //Initialize Driver	  
   WebDriver driver  = new FirefoxDriver();
   
   
   //Navigate to desired page
   driver.get("http://www.quackit.com/html/templates/frames/frames_example_3.html");
   
   //try to find an Element of frame menu
   try{
	   driver.findElement(By.linkText("Example 1"));
   }
   catch(NoSuchElementException e)
   {
	 System.out.println("Element with linkText 'Example 1' is not found");
   }
   
   //try to find an Element of frame topNav
   try{
	   driver.findElement(By.linkText("Menu 1"));
   }
   catch(NoSuchElementException e)
   {
	 System.out.println("'Menu 1' is not found");
   }
   
   //try to find an Element of frame topNav
   try{
	   driver.findElement(By.linkText("Quackit"));
   }
   catch(NoSuchElementException e)
   {
	 System.out.println("'Quackit' is not found");
   }
   
   //You can use getPageSource to verify whether driver will be able to find your element or not
   System.out.println(driver.getPageSource());
   
   //Now , we will switch to desired frame, here, menu frame
   driver.switchTo().frame(1);  //Using index, indexes are assigned internally to frame, based on order and list preferrable
   driver.switchTo().frame("menu");  //OR using name or id
   
   //try to find an Element of frame menu
   try{
	   driver.findElement(By.linkText("Example 1"));
   }
   catch(NoSuchElementException e)
   {
	 System.out.println("Element with linkText 'Example 1' is not found");
   }
   
   //Here you can use either of following to move back to default content, 
   //but remember that parentframe can be different if you have multiple hierarchy of frames 
   //driver.switchTo().parentFrame(); 
   driver.switchTo().defaultContent();
   
   //Now we will again switchTo() topNav Frame
   driver.switchTo().frame("topNav");
  
   try{
	   driver.findElement(By.linkText("Menu 1"));
   }
   catch(NoSuchElementException e)
   {
	 System.out.println("'Menu 1' is not found");
   }
  }
}