Tuesday, November 4, 2014


Frames is a web page within another web page. A web page can have one or more frames.

Each frame has its own unique numeric id and name. Selenium WebDriver uses this unique id or name to switch control between frames.





Syntax -

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

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

3. driver.switchTo().frame(WebElement arg0);


How To Switch Between Many Frames


import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Training {

 public static void main(String[] args) throws InterruptedException {

  WebDriver driver = new FirefoxDriver();
  driver.navigate().to(
    "http:// www.w3schools. com/js/tryit.asp?filename=tryjs_alert");

  driver.switchTo().frame(0);

  Thread.sleep(5000);
  WebElement element = driver.findElement(By.tagName("button"));
  element.click();

  Alert alert = driver.switchTo().alert();

  System.out.println(alert.getText());
  Thread.sleep(5000);
  alert.accept();
  driver.switchTo().defaultContent();

 }

}