2012-02-03 73 views
9

我正在編寫遺留應用程序的測試,其中主文檔中有一個iFrame,然後是其中的另一個iFrame。所以層次是:使用Selenium 2查找嵌套的iFrame

Html Div (id = tileSpace) 
    iFrame (id = ContentContainer) 
    iFrame (id = Content) 
     Elements 

這是我的代碼(我使用C#)

RemoteWebDriver driver = new InternetExplorerDriver(); 
var tileSpace = driver.FindElement(By.Id("tileSpace")); 
var firstIFrame = tileSpace.FindElement(By.Id("ContentContainer")); 
var contentIFrame = firstIFrame.FindElement(By.Id("Content")); 

的問題是,我無法達到第二級的iFrame即contentIFrame

任何想法?

回答

19

我目前正在類似網站上測試。 (主文檔中在嵌套的iframe)

<div> 
    <iframe> 
     <iframe><iframe/> 
    <iframe/> 
</div> 

看來你不使用API​​提供的frame switching method。這可能是問題所在。

這是我正在做的,它對我來說工作得很好。

//make sure it is in the main document right now 
driver.SwitchTo().DefaultContent(); 

//find the outer frame, and use switch to frame method 
IWebElement containerFrame = driver.FindElement(By.Id("ContentContainer")); 
driver.SwitchTo().Frame(containerFrame); 

//you are now in iframe "ContentContainer", then find the nested iframe inside 
IWebElement contentFrame = driver.FindElement(By.Id("Content")); 
driver.SwitchTo().Frame(contentFrame); 

//you are now in iframe "Content", then find the elements you want in the nested frame now 
IWebElement foo = driver.FindElement(By.Id("foo")); 
+0

謝謝!工作得很好。 – user356247 2012-02-06 11:34:58

0

試試下面的代碼:

//Switch to required frame 
    driver.SwitchTo().Frame("ContentContainer").SwitchTo().Frame("Content"); 

    //find and do the action on required elements 

    //Then come out of the iFrame 
    driver.SwitchTo().DefaultContent();