【问题标题】:How to switch to html within iframe using Selenium如何使用 Selenium 在 iframe 中切换到 html
【发布时间】:2021-11-14 04:42:39
【问题描述】:

我正在尝试单击扩展弹出窗口。 它在一个 iframe 中有 HTML,而另一个 iframe 在第一个 iframe 中有 HTML。

你真的可以使用 XPath 做到这一点吗?

<iframe id="1">
  <html>
    <iframe id="2">
     <html>

       This is where the element I need is.

     </html>
    </iframe>
  </html>
</iframe>

【问题讨论】:

    标签: java html selenium iframe


    【解决方案1】:

    它们被称为嵌套iframe,即iframeiframe 中。

    只需 switch第一个,然后 第二个

    driver.switchTo().frame(driver.findElement(By.id("1")));
    driver.switchTo().frame(driver.findElement(By.id("2")));
    driver.findElement(By.id("element id which you want to interact");
    

    记得在使用 iframe 后切换到默认内容。

    driver.switchTo().defaultContent();
    

    【讨论】:

    • 感谢您的回答。但是,如果有第二个非根 HTML 标记,这实际上不起作用。我不知道为什么。
    • 错误是什么?我可以有页面网址吗?
    • 这是我通过 ID 打开的扩展,第二个 HTML 中的所有内容对我来说都是无法访问的...i.imgur.com/R4kNJwn.png
    • 我只能看到那张图片中的一帧,也没有 id 为 1 或 2,因此您必须将其替换为 IFRAME_ID
    猜你喜欢
    • 2014-06-11
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 2015-02-11
    • 2018-01-15
    • 1970-01-01
    • 2020-11-23
    相关资源
    最近更新 更多