title: Accessing object in iframe using VBA
body: <p>To the point:</p>
  
  <p>I have successfully used VBA to do the following:</p>
  
  <ul>
  <li><p>Login to a website using getElementsByName</p></li>
  <li><p>Select parameters for the report that will be generated (using getelementsby...)</p></li>
  <li>generating the report after selecting parameters which renders the resulting dataset into an iframe on the same page</li>
  </ul>
  
  <p><strong>Important to note - The website is client-side</strong></p>
  
  <p>The above was the simple part, the difficult part is as below:</p>
  
  <blockquote>
    <p><strong>clicking on a gif image within the iframe that exports the dataset to a csv</strong></p>
  </blockquote>
  
  <p>I have tried the following:</p>
  
  <pre><code>Dim idoc As HTMLDocument
  Dim iframe As HTMLFrameElement
  Dim iframe2 As HTMLDocument
  
  Set idoc = objIE.document
  Set iframe = idoc.all("iframename")
  Set iframe2 = iframe.contentDocument
  
      Do Until InStr(1, objIE.document.all("iframename").contentDocument.innerHTML, "img.gif", vbTextCompare) = 0
          DoEvents
      Loop
  </code></pre>
  
  <p>To give some context to the logic above -</p>
  
  <ul>
  <li>I accessed the main frame</li>
  <li>i accessed the iframe by its name element</li>
  <li>i accessed the content within the iframe</li>
  <li><strong>I attempted to find the gif image that needs to be clicked to export to csv</strong></li>
  </ul>
  
  <p>It is at this line that it trips up saying "Object doesn't support this property or method"</p>
  
  <p>Also tried accessing the iframe gif by the a element and href attribute but this totally failed. I also tried grabbing the image from its source URL but all this does it take me to the page the image is from.</p>
  
  <p><strong>note: the iframe does not have an ID and strangely the gif image does not have an "onclick" element/event</strong></p>
  
  <blockquote>
    <p><strong>Final consideration - attempted scraping the iframe using R</strong></p>
  </blockquote>
  
  <p>accessing the HTML node of the iframe was simple, however trying to access the attributes of the iframe and subsequently the nodes of the table proved unsuccessful. All it returned was "Character(0)"</p>
  
  <pre><code>library(rvest)
  library(magrittr)
  
  Blah &lt;-read_html("web address redacted") %&gt;%
    html_nodes("#iframe")%&gt;%
    html_nodes("#img")%&gt;%
    html_attr("#src")%&gt;%
    #read_html()%&gt;%
    head()
  Blah
  </code></pre>
  
  <p>As soon as a i include read_html the following error returns on the script:</p>
  
  <p>Error in if (grepl("&lt;|>", x)) { : argument is of length zero</p>
  
  <p>I suspect this is referring to the Character(0) </p>
  
  <p>Appreciate any guidance here!</p>
  
  <p>Many Thanks,</p>
  
  <blockquote>
    <p>HTML</p>
  </blockquote>
  
  <pre><code>&lt;div align="center"&gt; 
      &lt;table id="table1" style="border-collapse: collapse" width="700" cellspacing="0" cellpadding="0" border="0"&gt; 
          &lt;tbody&gt;
              &lt;tr&gt;
                  &lt;td colspan="6"&gt; &amp;nbsp;&lt;/td&gt;
              &lt;/tr&gt; 
              &lt;tr&gt; 
                  &lt;td colspan="6"&gt; 
                      &lt;a href="href redacted"&gt;
                          &lt;img src="img.gif" width="38" height="38" border="0" align="right"&gt;
                      &lt;/a&gt;
                      &lt;strong&gt;x - &lt;/strong&gt;
                  &lt;/td&gt;
              &lt;/tr&gt; 
          &lt;/tbody&gt;
      &lt;/table&gt;
  &lt;/div&gt;
  </code></pre>