【问题标题】:How to switch to and from in Selenium [duplicate]如何在 Selenium 中切换 [重复]
【发布时间】:2015-10-19 10:02:22
【问题描述】:

我正在尝试导航到 iframe 并使用 selenium 返回顶部框架。

<html>
    <body>
        <div id="testID">This is upper frame</div>
        <iframe src="test.html"></iframe>
     </body>
</html>

我想获得使用driver.findElement(By.id("testID")) 轻松获得的testID。

然后我想使用 driver.switchTo().frame(driver.findElement(By.id("new_page"))); 导航到 iframe 其中new_pagetest.htm 中的id。

现在,我的要求是切换回来,以便我可以再次从上面的正文中读取testID

但如果我使用: driver.switchTo().frame(driver.findElement(By.id("testID"))); 我得到了这样的例外:

无法定位元素

【问题讨论】:

    标签: java selenium iframe


    【解决方案1】:

    切换到框架:

    driver.switchTo().frame(0);
    

    要切换回主窗口:

    driver.switchTo().defaultContent();
    

    另外我建议先找frame的元素做switch再找元素。

    您可以通过以下3种方式切换到框架:

    按帧索引

    按框架 ID 或名称

    按框架 WebElement

    更多详情请查看:Switch to Frame in Selenium

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2020-11-07
      • 2019-07-20
      • 2020-10-06
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      • 1970-01-01
      • 2021-11-14
      • 2015-02-11
      相关资源
      最近更新 更多