使用Selenium 2查找嵌套的iFrame

我正在为遗留应用程序编写测试,其中主文档中有一个iFrame,然后是其中的另一个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

有任何想法吗?

我目前正在类似的网站上进行测试。 (主文档中嵌套的iframe)

 

您似乎没有使用Api中提供的帧切换方法 。 这可能是问题所在。

这是我正在做的,它对我来说很好。

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

试试以下代码:

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