How to handle IFRAME or IFRAMES with selenium web driver

In this tutorial we will learn  handling iframes using selenium web driver .

To switch between iframes we have to use the driver's  switchTo().frame() command.

swichTo().frame() :  

  We can use this method to change web driver class object focus from active page to specified frame in that page.

driver.swichTo().frame(" name / index ");

Note 1 :  In general <iframe> tage is having name attribute. If there is no name to any frame, then we can use index.

Note 2 : While assigning indexes to frames in a page source code we need to follow below type of indexing.

          ----------------------

       ----------------------------- 

          0  <iframe  --------------------  >

            < /iframe > 

         1   <iframe  --------------------  >       driver.switchTo().frame(1);  // it is go to 2nd frame in page

                                                                                

                    0 <iframe  --------------------  >     driver.switchTo().frame(0);

                   < /iframe > 

                    1 <iframe  --------------------  >

                     < /iframe > 

 

      < /iframe > 

 

switchTo().parentFrame() :   We can use this method to change focus from current frame to parent frame.   

switchTo().defaultContent() : We can use this method to change focus from current frame to page

Example:

           driver.findElement(By.name("userid")).sendKeys(" raju " );  

            driver.switchTo().frame(frame1);

         driver.findElement(By.name("password")).sendKeys(" xxxxx " );  

          driver.switchTo().defaultContent();

        driver.findElement(By.name("ok")).click( );