Handling Multiple Frames using Selenium Webdriver


Frames is the html document which is embedded on another html document.So page on the page is frames.Generally Frames are used in web page when there are dynamic changes in webpage.Instead of loading whole webpage only frame are loaded.

Ex: Breaking news in news webpage.

How to Automate Frames:

Example 1:


When we are dealing with frames we have use switchTo() to  Frame()  method.

Syntax:  driver.switchTo().frame();

Handling Multiple Frames














Here in above image we have show you how to switch from frame1 to frame2 when two frame are different.

Step 1:
         First driver focus will be on normal webpage,So we have to switch from webpage to Frame1.
           
            driver.switchTo.frame("frame1");

Step 2:
        Now the driver focus is on frame1.if we want to switch from frame1 to frame2 then we have to switch to default content i,e webpage.
          
          driver.switchTo.defaultcontent();

Step3:
       Now the focus is on webpage again we have to switch from webpage to Frame2 now.
     
      driver.switchTo.frame("frame2");
            


        

Example 2:

















Here in above image we have show you how to switch from frame1 to frame2 when two frame are embedded.that means frame within frame. So in this case steps would be.


Step 1:
         First driver focus will be on normal webpage,So we have to switch from webpage to Frame1.
           
            driver.switchTo.frame("frame1");

Step 2:
        Now the driver focus is on frame1.if we want to switch from frame1 to frame2 then we can         directly switch to frame2. In this case no need to switch to default page (or) Normal webpage.
          
          driver.switchTo.frame("frame2");


Example:

public class Framesexp1

 {

WebDriver driver;
 

@Test
public void frame1() throws InterruptedException
{
           driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://netbanking.hdfcbank.com/netbanking/");
driver.manage().window().maximize();
int number=frameNumber(By.xpath("//input[@class='input_password']"));
driver.switchTo().frame(number);
driver.findElement(By.xpath("//input[@class='input_password']")).sendKeys("123123");
int number1=frameNumber(By.xpath("//a[@href='javascript:void(0)']"));
driver.switchTo().frame(number1);
driver.findElement(By.partialLinkText("Know")).click();
}
        // This is generic method which can be used for frames.
public int frameNumber(By by)
{
int i;
int framecount=

               driver.findElements(By.tagName("frameset")).size();

for(i=0;i<framecount;i++)
{
driver.switchTo().frame(i);
int count=driver.findElements(by).size();

    if(count>0)
{
break;
}
}
driver.switchTo().defaultContent();
return i;
}
}





No comments:

Post a Comment