简体   繁体   中英

How to perform actions on child iFrame using Selenium?

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);

Try this snippet.

 //Switch To parent frame "iframe1"
 WebElement parentFrame = driver.findElement(By.id("iframe1"));
 driver.switchTo.frame(parentFrame );

 //Switch To first child frame "iframe2"
 WebElement firstChildFrame = driver.findElement(By.id("iframe2"));
 driver.switchTo.frame(firstChildFrame ); 

 //Do some actions on iframe2
 //Switch To Top (or) Parent frame by using
 driver.switchTo().defaultContent();

 //Switch To parent frame "iframe1"
 WebElement parentFrame = driver.findElement(By.id("iframe1"));
 driver.switchTo.frame(parentFrame );

 //Switch To second child frame "iframe3"
 WebElement secondChildFrame = driver.findElement(By.id("iframe3"));
 driver.switchTo.frame(secondChildFrame); 

For more details on selecting frames use this http://darrellgrainger.blogspot.ca/2012/04/frames-and-webdriver.html .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM