VBA IE Automation - Read iFrame

2019-01-15 08:59发布

问题:

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

回答1:

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



回答2:

Try this...

Dim elemCollection As IHTMLElementCollection

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


回答3:

Thanx abhinov.....

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

This did the JOB for Me.....



回答4:

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)?



回答5:

Try to use:

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