How to handle iFrames in Selenium?


A basic example of handling frames at www.espncricinfo.com

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Abc
{
public static void main(String args[])
{
WebDriver driver=new FirefoxDriver();
driver.get("http://www.cricinfo.com/");
driver.manage().timeouts().implicitlyWait(3000, TimeUnit.MILLISECONDS);
driver.findElement(By.linkText("Countries")).click();
driver.switchTo().frame(driver.findElement(By.xpath("//*[@id='ciHomeContentrhs']/iframe")));
driver.findElement(By.linkText("Cricinfo")).click();
}
}