Aaron Contreras
Aaron Contreras

Reputation: 645

VBA IE Automation - Read iFrame

Our business uses a browser-based program for operations. I'm automating a solution to navigate through this site, and retrieve some data at the end.

The site itself uses regular frames very heavily. However, at the very end of my process, it populates my data not into a frame, but an iFrame. There's also very extensive javascript throughout the site, making things muddy.

Grabbing the iFrame's src URL and opening in a new browser errors out the page (i.e. the page displays error text instead of the content).

My question:

How can I grab the text out of an iFrame through VBA?

What I've tried so far (feel free to skip):

Target a specific iFrame in a specific frame, and grab innerHTML

With ie.document.frames(myFrameNum).document.getElementsByTagName("iframe")(1).document.body
stringResult = .innerHTML

Target a specific iFrame by ID in a specific frame, and grab innerHTML

Dim iFrm As HTMLIFrame
Set iFrm = ie.document.frames(myFrameNum).document.getElementByID("iFrameID")
Debug.Print iFrm.document.body.innerText

Find any instances of iFrames, and grab them (No results - perhaps because the iframe is embeded in a frame?)

Dim iFrm As HTMLIFrame
Dim doc As HTMLDocument
 For iterator = 0 To ie.document.all.Length - 1
  If TypeName(ie.document.all(iterator)) = "HTMLIFrame" Then
   Set iFrm = ie.document.all(iterator)
   Set doc = iFrm.document
   Debug.Print & doc.body.outerHTML
  End If
Next

Upvotes: 10

Views: 33740

Answers (4)

Sukesh
Sukesh

Reputation: 13

I faced a similar problem, finally solved it using:

ObjIE.document.frames(0).document.forms(0).innerText

Note: The text which I was in need was present in form which is located in a iframe.

Am new to VBA, can some body explain what exactly 0 in Frames(0)/forms (0) is?

If it something like Frame index or frame number(As of my assumption) please let me know how can we find frame index(in any HTML)?

Upvotes: 0

Rajesh
Rajesh

Reputation: 71

Try this...

Dim elemCollection As IHTMLElementCollection

Set elemCollection = objDoc.frames("iFrameID").document.all
Debug.Print elemCollection.Item("pagemenuli-adv").innerText

Upvotes: 7

abhinov
abhinov

Reputation: 154

I faced the same issue and I got the solution using the following line of script..

IE.Document.getElementsbyTagName("iframe")(0).contentDocument.getElementsbyTagName("body")(0).innertext

Upvotes: 10

Michał Dziok
Michał Dziok

Reputation: 11

Try to use:

`Dim iFrm As HTMLIFrame
Set iFrm = ie.document.frames(myFrameNum).document.getElementByID("iFrameID")
Debug.Print iFrm.contentDocument.body.innerHTML`

Upvotes: -1

Related Questions