【问题标题】:How to get all the iframes in a page如何获取页面中的所有 iframe
【发布时间】:2014-07-23 16:21:32
【问题描述】:

我有一个非常复杂的 html 页面。

<html>
 <head></head>
  <body>
<iframe> A </iframe>
  <table>
   <div>
    <span>
      <div> <iframe> B </iframe> </div>
        <style>
          <div>
            <div>

当我尝试在 iframe A 中定位一个元素时,它会到达另一个 iframe。我尝试使用 driver.switchTo().frame(0) 切换到 iframe,但没有用。

当我尝试使用 findElements 方法获取所有 iframe 时,它​​只返回一个 iframe (B)。

【问题讨论】:

    标签: html iframe selenium


    【解决方案1】:

    答案取决于您想对 iframe 列表做什么。

    如果你想要所有的列表,你可以尝试使用递归函数。

    public void getAlliFrames(){
       List<WebElement> iframes = driver.findElements(By.tagName("iframe"));
    
       if(iframe.count() > 0){
          driver.switchTo().frame(iframe.get(0));
    
          getAlliFrames();
       }else{
          System.out.println("No more iframes");
       }
    }
    

    PS:这是未经测试的代码,仅供参考。

    【讨论】:

    • 感谢您的建议......我想通了,现在我可以更进一步...... :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-27
    相关资源
    最近更新 更多