SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

How To Handle/Working With IFrame In Selenium WebDriver

Working with Iframes In webdriver was pending post from long time. Earlier we learnt, how to switch between windows In THIS POST. Also we learnt trick to switching between window tabs In THIS POST. If you are working with selenium IDE then you can use "selectframe" command to select and switch to IFrame as described In THIS POST.

Working with Iframe and page content
IFrame Is another web element and you can not locate Its element directly In selenium webdriver. To work with IFrame element In selenium webdriver, first of all you need to select that IFrame using syntax like bellow.

//switch to frame1. frame1 Is ID of frame.
driver.switchTo().frame("frame1");

Now you can work with any element which Is Inside frame1. Now supposing you wants to switch back to page content then you need to use syntax like bellow.
//Switch back to page content.
driver.switchTo().defaultContent();

After above syntax execution, You can work with page elements.

Working with multiple frames on same page
If there are multiple Iframes on single page then you can not directly navigate from Iframe1 to IFrame2. For that, You need to select page In between as bellow.
//switch to frame1
driver.switchTo().frame("frame1");
driver.findElement(By.xpath("//td[contains(text(),'Cow')]/preceding-sibling::td/input[@type='checkbox']")).click();
  
//Switch back to page content.
driver.switchTo().defaultContent();
  
//switch to frame2
driver.switchTo().frame("frame2");
driver.findElement(By.xpath("//input[@value='Boat']")).click();

Full example to switch from page to IFrame, Iframe to page and Iframe to Iframe Is as bellow.
package Testing_Pack;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Iframes {
 WebDriver driver;
 @BeforeTest
 public void setup() throws Exception {
  driver =new FirefoxDriver();     
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.get("http://only-testing-blog.blogspot.in/2015/01/iframe1.html");
 }
 
 @Test
 public void handleFrames(){
  //Working with page element.
  //Inserting some text In Town textbox of page.
  driver.findElement(By.xpath("//input[@name='Town']")).sendKeys("Your town");
  
  //Working with Iframe1 elements
  //switch to frame1 and select cow checkbox from table. frame1 Is ID of frame.
  driver.switchTo().frame("frame1");
  driver.findElement(By.xpath("//td[contains(text(),'Cow')]/preceding-sibling::td/input[@type='checkbox']")).click();
  
  //Switch back to page content.
  driver.switchTo().defaultContent();
  
  //Working with Iframe2 elements
  //switch to frame2 and select I have a boat checkbox.  frame2 Is ID of frame.
  driver.switchTo().frame("frame2");
  driver.findElement(By.xpath("//input[@value='Boat']")).click();
  
  //switch back to page to Inserting some text In Country textbox of page.
  driver.switchTo().defaultContent();
  driver.findElement(By.xpath("//input[@name='Country']")).sendKeys("your country");
 }
}

This way, you can handle IFrames In selenium webdriver.

8 comments:

  1. How to find it is a frame and Frame id ?

    ReplyDelete
    Replies
    1. you can use firebug to find iframe id

      Delete
  2. How to find iframe details
    - iframe name, id, xpath and objects on the iframe and it's xpath's

    e.g.
    When I have used FireBug to find the iframe details I got ID as //*[@id="x173e0d8-49y7-4zz-b6ee-d3ab0e6x31z6"]/div[3]/iframe

    Question is how to switch to iframe

    getting error as
    Execution executed ... org.openqa.selenium.NoSuchElementException: Unable to find element with id

    ReplyDelete
    Replies
    1. Check and make sure ID of IFrame remain same or It Is dynamic..

      How to check
      1. get ID of iframe using firebug and paste It In notepad.
      2. reload page and reopen IFrame
      3. get ID of iframe using firebug and paste It In notepad.
      4. Compre both ID.
      If both are same then It Is static and you can use ID In XPath. If both are different that means your IFrame ID Is dynamic and you need to use something else like name In XPath or use absolute XPath.

      Delete
  3. I'm having an application that is develop in Frame, again Frame, again

    inside a Freame... as mentioned belwo:
    //did_summaryPane frameset
    //did_workPane frameset
    //did_menuframe

    //Div = did_appframe_display
    //ifrom = did_appframe
    //frameset = did_mainPane
    //frameset = did_workPane
    //frameset = did_myBarPane
    //frameset = did_menuframe
    //frame = work
    //frameset = did_criteria
    //frame = work_top
    //cust_lname
    The contstranint is application open only & only Internet Explorer and

    I have to develop it either Selenium or any Free Automation tool

    using C#, Kindly suggest me.

    Regards,
    Ikram
    9899621146

    ReplyDelete
  4. So can anyone explain how to scroll in a page when you have an iframe and you want to locate an element which is in the iframe but not in the current view of the webdriver page?

    ReplyDelete