title: How to get all nested iframes on document?
body: <p>I found many related questions but none of them had a solution that worked for me, so apologies if this is a dupe.</p>
  
  <p>I have the following HTML structure (simplified) :</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;/head&gt;
  &lt;body style=""&gt;
      &lt;div&gt;
          &lt;div&gt;
              &lt;div&gt;
                  &lt;iframe&gt;
                      &lt;html&gt;
                          &lt;head&gt;&lt;/head&gt;
                          &lt;body&gt;
                              &lt;div&gt;&lt;/div&gt;
                              &lt;iframe&gt;
                                  &lt;html&gt;
                                      &lt;head&gt;&lt;/head&gt;
                                      &lt;body&gt;
                                          &lt;div&gt;
                                              &lt;iframe src="about:blank"&gt;
                                                  &lt;html&gt;
                                                      &lt;head&gt;&lt;/head&gt;
                                                      &lt;body&gt;
                                                          &lt;img /&gt;
                                                          &lt;iframe id="some_random_id"&gt;
                                                              &lt;html&gt;
                                                                  &lt;head&gt;&lt;/head&gt;
                                                                  &lt;body&gt;
                                                                      &lt;div&gt;
                                                                          &lt;!-- main content --&gt;
                                                                      &lt;/div&gt;
                                                                  &lt;/body&gt;
                                                              &lt;/html&gt;
                                                          &lt;/iframe&gt;
                                                      &lt;/body&gt;
                                                  &lt;/html&gt;
                                              &lt;/iframe&gt;
                                          &lt;/div&gt;
                                      &lt;/body&gt;
                                  &lt;/html&gt;
                              &lt;/iframe&gt;
                          &lt;/body&gt;
                      &lt;/html&gt;
                  &lt;/iframe&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And I would like to retrieve all the iframes, ideally in an array.
  I have tried the following:</p>
  
  <pre><code>document.getElementsByTagName('iframe')
  </code></pre>
  
  <p>But that returns an array of size 1 : <code>[iframe]</code></p>
  
  <p><code>window.frames.length</code> give me 1</p>
  
  <p>I thought about doing something like :</p>
  
  <pre><code>var a = document.getElementsByTagName('iframe')[0]
  var b = a.getElementsByTagName('iframe')[0] 
  // b is undefined
  var b = a.contentDocument.getElementsByTagName('iframe')[0]
  // b is undefined
  </code></pre>
  
  <p>Is there any way to retrieve all iframe on the page? Alternatively, just getting the last one (the one with the id <code>some_random_id</code>) would works as fine, but I can't use the id to select it since the html is created by a third party.</p>
  
  <p><strong>Edit:</strong> I don't think my question is a duplicate of <a href="https://stackoverflow.com/questions/11144479/using-document-getelementsbytagname-on-a-page-with-iframes-elements-inside-the">using document.getElementsByTagName on a page with iFrames - elements inside the iframe are not being picked up</a>
  because the accepted answer in this question use:</p>
  
  <pre><code>for( j=0; j&lt;m; j++) {
      ...
  } 
  </code></pre>
  
  <p>Where <code>m</code> is <code>document.getElementsByTagName('iframe').length</code>. But in my case it would have the value <code>1</code> and thus I couldn't access the nested iframes.</p>