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.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

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

//switch to the frame having name = fname                        driver.switchTo().frame("fname").findElement(By.id("dd")).clear();
                          
//switch to the frame having id = fid
WebElement e = driver.findElement(By.id("fid"));
driver.switchTo().frame(e).findElement(By.id("dd")).clear();
                          
                           //driver.navigate();
                         //driver.navigate("http://www.google.com");
                          
              }catch(Exception e){}
                    
                    
              finally{
                     driver.close();
                     driver.quit();
              }     
                    
             
             
             
             
       }
      
      

      
}


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

No comments:

Post a Comment

Buy Best Selenium Books

Contributors