How to handle iframe in Selenium WebDriver using java

<div>    
  <iframe id="cq-cf-frame ">    
    <iframe id="gen367">   
      <body spellcheck="false" id="CQrte" style="height: 255px; font-size: 12px; font-family:tahoma,arial,helvetica,sans-serif; background-image: url(&quot;/libs/cq/ui/widgets/themes/default/ext/form/text-bg.gif&quot;); background-repeat: repeat-x; background-attachment: fixed;">
        <p>4t43t4<br></p>
      </body >
    </iframe>
  </iframe>    
</div> 

In this scenario there is an iframe under iframe. And I have to select the outer iframe to go to inner iframe and write in the body which is in the inner iframe.

Next, I have to come out from the inner iframe to outer iframe and click on OK button, (which is in the outer iframe).

Following is my code

/*Line 1 */ driver.switchTo().frame("cq-cf-frame");
/*     2 */ driver.findElement(By.css("#extdd-9 > div.tblRow >  input.edititem").click();
/*     3 */ driver.switchTo().Frame("cq-gen379");
/*     4 */ driver.findElement(By.id("CQrte").sendKeys("Tnx");  
/*     5 */ selenium.selectFrame("relative=up");       
/*     6 */ driver.findElement(By.xpath("//button[text()='OK']")).click(); 

Following is my problem:

My test code is working fine up to line number 4 i.e. writing into the body, but I want to come out from inner to outer iframe it says that the element //button[text()='OK'] not found.

I tried with using index, parent, relative, but had no luck.

NOTE: If I don’t select the inner frame (cq-gen379). I'm able to click on the OK button.


Solution 1:

In Webdriver, you should use driver.switchTo().defaultContent(); to get out of a frame. You need to get out of all the frames first, then switch into outer frame again.

// between step 4 and step 5
// remove selenium.selectFrame("relative=up");
driver.switchTo().defaultContent(); // you are now outside both frames
driver.switchTo().frame("cq-cf-frame");
// now continue step 6
driver.findElement(By.xpath("//button[text()='OK']")).click(); 

Solution 2:

You have to get back out of the Iframe with the following code:

driver.switchTo().frame(driver.findElement(By.id("frameId")));
//do your stuff
driver.switchTo().defaultContent();

Solution 3:

You need to first find iframe. You can do so using following statement.

WebElement iFrame= driver.findElement(By.tagName("iframe"));

Then, you can swith to it using switchTo method on you WebDriver object.

driver.switchTo().frame(iFrame);

And to move back to the parent frame, you can either use switchTo().parentFrame() or if you want to get back to the main (or most parent) frame, you can use switchTo().defaultContent();.

driver.switchTo().parentFrame();    // to move back to parent frame
driver.switchTo().defaultContent(); // to move back to most parent or main frame

Hope it helps.

Solution 4:

To get back to the parent frame, use:

driver.switchTo().parentFrame();

To get back to the first/main frame, use:

driver.switchTo().defaultContent();

Solution 5:

WebDriver driver=new FirefoxDriver();
driver.get("http://www.java-examples.com/java-string-examples");
Thread.sleep(3000);
//Switch to nested frame
driver.switchTo().frame("aswift_2").switchTo().frame("google_ads_frame3");