c# - 使用 Selenium 2 查找嵌套 iFrame

标签 c# selenium webdriver selenium-webdriver

我正在为旧应用程序编写测试,其中主文档中有一个 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/

相关文章:

testing - 如何处理 HTML 中的动态 ID

c# - 有没有好的方法来获取当前的属性名称?

c# - 如何从句子中所有数字的每个单词中获取第一个字符?

c# - 如何将任意 json 对象发布到 webapi

java - 获取失败断言的描述

java - 如何访问抽象类的 protected 方法?

c# - 模型中文本框的默认值

python - 使用 PhantomJS 和 Selenium 抓取网络数据

python - 当我调用以下名为 find_element_by_xpath 的函数时到底发生了什么?

WebDriver - Chrome - 排除开关列表