How To Handle/Working With IFrame In Selenium WebDriver

Working amongst Iframes In webdriver was pending post service from long time. Earlier nosotros learnt, how to switch betwixt windows In THIS POST. Also nosotros learnt play a joke on to switching betwixt window tabs In THIS POST. If y'all are working amongst selenium IDE thus y'all tin piece of job "selectframe" ascendance to direct together with switch to IFrame as described In THIS POST.

Working amongst Iframe together with page content
IFrame Is approximately other spider web chemical cistron together with y'all tin non locate Its chemical cistron straight In selenium webdriver. To work amongst IFrame chemical cistron In selenium webdriver, showtime of all y'all quest to direct that IFrame using syntax similar bellow.

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

Now y'all tin piece of job amongst whatever chemical cistron which Is Inside frame1. Now supposing y'all wants to switch dorsum to page content thus y'all quest to piece of job syntax similar bellow.
//Switch dorsum to page content. driver.switchTo().defaultContent();

After higher upwards syntax execution, You tin piece of job amongst page elements.

Working amongst multiple frames on same page
If at that spot are multiple Iframes on unmarried page thus y'all tin non straight navigate from Iframe1 to IFrame2. For that, You quest to direct page In betwixt equally bellow.
//switch to frame1 driver.switchTo().frame("frame1"); driver.findElement(By.xpath("//td[contains(text(),'Cow')]/preceding-sibling::td/input[@type='checkbox']")).click();    //Switch dorsum to page content. driver.switchTo().defaultContent();    //switch to frame2 driver.switchTo().frame("frame2"); driver.findElement(By.xpath("//input[@value='Boat']")).click();

Full illustration to switch from page to IFrame, Iframe to page together with Iframe to Iframe Is equally 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;  world cast Iframes {  WebDriver driver;  @BeforeTest  world void setup() throws Exception {   driver =new FirefoxDriver();        driver.manage().window().maximize();   driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);   driver.get(" ");  }    @Test  world void handleFrames(){   //Working amongst page element.   //Inserting approximately text In Town textbox of page.   driver.findElement(By.xpath("//input[@name='Town']")).sendKeys("Your town");      //Working amongst Iframe1 elements   //switch to frame1 together with direct moo-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 dorsum to page content.   driver.switchTo().defaultContent();      //Working amongst Iframe2 elements   //switch to frame2 together with direct I accept a boat checkbox.  frame2 Is ID of frame.   driver.switchTo().frame("frame2");   driver.findElement(By.xpath("//input[@value='Boat']")).click();      //switch dorsum to page to Inserting approximately text In Country textbox of page.   driver.switchTo().defaultContent();   driver.findElement(By.xpath("//input[@name='Country']")).sendKeys("your country");  } }

This way, y'all tin handle IFrames In selenium webdriver.

More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM

http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post