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.
No comments:
Post a Comment