使用 Selenium 2 查找嵌套的 iFrame [英] Finding nested iFrame using Selenium 2

查看:26
本文介绍了使用 Selenium 2 查找嵌套的 iFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个遗留应用程序编写测试,其中主文档中有一个 iFrame,然后在其中有另一个 iFrame.所以层次结构是:

I am writing tests for a legacy application in which there is an iFrame within the main document, and then another iFrame within that. So the hierarchy is:

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

这是我的代码(我使用的是 C#)

This is my code (I am using 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

The problem is, I am unable to reach the 2nd level iFrame i.e. contentIFrame

有什么想法吗?

推荐答案

我目前正在一个类似的网站上进行测试.(主文档内的嵌套 iframe)

I'm currently testing on a similar website. (nested iframes inside the main document)

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

您好像没有使用帧切换方式 在 Api 中提供.这可能是问题所在.

It seems that you are not using the frame switching method provided in Api. This could be the problem.

这就是我正在做的,对我来说效果很好.

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

这篇关于使用 Selenium 2 查找嵌套的 iFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆