Friday, 21 February 2014

How to switch to frame in Selenium Webdriver in Java?

A webpage may contain multiple frames. To perform the operation on elements inside frame, we need to first switch to that frame and then we can use Webdriver methods on the web elements inside frame.

We can switch to frame using 3 different ways.

  1. using index
  2. using name
  3. using any identification method like xpath, cssselector.

package seleniumtest;

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

public  class MainTest {
       public static void main(String[] args) {
                 WebDriver driver =null;
                 System.setProperty("", "F:\\selenium\\csharp\\chromedriver.exe");
                 driver = new ChromeDriver();
//switch to the first frame in document                           driver.switchTo().frame(0).findElement("dd")).clear();

//switch to the frame having name = fname                        driver.switchTo().frame("fname").findElement("dd")).clear();
//switch to the frame having id = fid
WebElement e = driver.findElement("fid"));
              }catch(Exception e){}


What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at

No comments:

Post a Comment

Buy Best Selenium Books