title: Using selenium, I need to iterate over all iframes and their child iframes on the fly
body: <p><strong>I need to iterate over all iframes and their child iframes on the fly as I want to perform some action on all the <code>&lt;img&gt;</code> tags. I am using selenium.</strong></p>
  
  <pre><code>public class logic {
  
  static WebDriver driver;
  static List&lt;WebElement&gt; TotaliinnerFrames=null;
  
  logic() 
  {
      driver=new FirefoxDriver();
      driver.get("aaaa.html");
  }
  
  public static void main(String[] args) 
  {
      logic log=new logic();
      find_parent_iFrame();
  }
  
  private static void find_parent_iFrame() 
  {
      List&lt;WebElement&gt; TotaliFrames =  driver.findElements(By.tagName("iframe"));
      System.out.println("TotaliFrames\t"+TotaliFrames.size());
  
      for(WebElement frame:TotaliFrames)
      {
          System.out.println("calling main frame==============================&gt;\t");
          findLandingPage(frame);
      }
  
      if(TotaliFrames.size()&gt;0)
      {
          driver.switchTo().defaultContent();
      }
      else
      {
          List&lt;WebElement&gt; canvas = driver.findElements(By.tagName("canvas"));
          List&lt;WebElement&gt; img = driver.findElements(By.tagName("img"));
          List&lt;WebElement&gt; anchor = driver.findElements(By.tagName("a")); 
          if(canvas.size()&gt;0)
          {
              System.out.println("inside canvas");
  
              for(WebElement canvTemp : canvas)
              {
                  callClick(canvTemp);
              }
          }
  
          if(img.size()&gt;0)
          {
              System.out.println("inside img");
              for(WebElement imge : img)
              {
                  callClick(imge);
              }
          }
  
          if (anchor.size()&gt;0)
          {
              System.out.println("inside anchor");
              for(WebElement anchorc : anchor)
              {
                  callClick(anchorc);
              }
          }
  
      }
  }
  
  private static void findLandingPage(WebElement frame) 
  {
      driver.switchTo().frame(frame);
  
      List&lt;WebElement&gt; canvas = driver.findElements(By.tagName("canvas"));
      List&lt;WebElement&gt; img = driver.findElements(By.tagName("img"));
      List&lt;WebElement&gt; anchor = driver.findElements(By.tagName("a")); 
      System.out.println("canvas\t"+canvas.size());
      System.out.println("img\t"+img.size());
      System.out.println("anchor\t"+anchor.size());
  
      if(canvas.size()&gt;0)
      {
          System.out.println("inside canvas");
  
          for(WebElement canvTemp : canvas)
          {
              callClick(canvTemp);
          }
      }
  
      if(img.size()&gt;0)
      {
          System.out.println("inside img");
          for(WebElement imge : img)
          {
              callClick(imge);
          }
      }
  
      if(anchor.size()&gt;0)
      {
          System.out.println("inside anchor");
          for(WebElement anchorc : anchor)
          {
              callClick(anchorc);
          }
      }
  
      TotaliinnerFrames = driver.findElements(By.tagName("iframe"));
      System.out.println("TotaliinnerFrames\t"+TotaliinnerFrames.size());
      int innerframesize=TotaliinnerFrames.size();
      if(innerframesize&gt;0)
      {
          System.out.println("TotaliinnerFrames\t"+innerframesize);
          for(WebElement inframe:TotaliinnerFrames)
          {
              System.out.println("calling inner recursive\t");
              findLandingPage(inframe);
          }
          TotaliinnerFrames.clear();
      }
      else
      {
          driver.switchTo().defaultContent();
      }
  }
  
  private static void callClick(WebElement canvTemp) 
  {
      try
      {
          canvTemp.click();
      }
      catch(Exception e)
      {
          System.out.println("Exception unable to click");
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <h2> <strong>I need to Iterate over all iframe and its child iframe on the fly i want all tags and perform some action. using selenium.</strong></h2>