我正在为旧应用程序编写测试,其中主文档中有一个 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)
<div>
<iframe>
<iframe><iframe/>
<iframe/>
</div>
您似乎没有使用 frame switching method 在 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"));
关于c# - 使用 Selenium 2 查找嵌套 iFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9130871/