How to perform actions on the child IFrame using Selenium?

advertisements

How can we perform actions on iFrame which is in a iFrame?

HTML:

<div id="div1">
 <iframe id="iframe1">
   <div id="div2">
     <iframe id="iframe2">
     </iframe>
   </div>
   <div id="div3">
     <iframe id="iframe3">
     </iframe>
   </div>
 </iframe>
</div>

I need to perform actions on iframe2 and iframe3. Tried with the following code:

1. driver.switchTo().frame("iframe1.0.iframe2");
2. WebElement firstFrame = driver.findElement(By.id("iframe1"));
   driver.switchTo().frame(firstFrame);
   WebElement secondFrame = driver.findElement(By.id("iframe2"));
   driver.switchTo().frame(secondFrame);

but couldn't resolve the issue.

Please suggest if any other method to be followed.


Try like this

//switch the control to first frame
WebElement firstFrame = driver.findElement(By.id("iframe1"));
driver.switchTo().frame(firstFrame);

//switch the control to second frame
WebElement secondFrame = driver.findElement(By.id("iframe2"));
driver.switchTo().frame(secondFrame);

//Switch back the control to first frame before switch it to frame3
driver.switchTo().frame(firstFrame);

WebElement thirdFrame = driver.findElement(By.id("iframe3"));
driver.switchTo().frame(thirdFrame);