简体   繁体   English

如何使用Selenium对子iFrame执行操作?

[英]How to perform actions on child iFrame using Selenium?

How can we perform actions on iFrame which is in a iFrame? 我们如何在iFrame中的iFrame上执行操作?

HTML: 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. 我需要对iframe2和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 . 有关选择框架的更多详细信息,请使用此http://darrellgrainger.blogspot.ca/2012/04/frames-and-webdriver.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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