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
You can use VBA to extract data from web pages, either as whole tables or by parsing the underlying HTML elements. This blog shows you how to code both methods (the technique is often called "web-scraping").
VBA macro drives internet explorer using its DOM properties and methods. The macro allows to interact with web page controls (text fields and buttons).
Although VBA was declared legacy in 2008, this implementation of Visual Basic can help you automate the repetitive tasks in your daily life. The language is object oriented, it's written in C++, and it includes all the features you would expect in a coding language these days.
Step 1) Open an Excel-based Macro and access the developer option of excel. Step 2) Select Visual Basic option under Developer ribbon. Step 3) Insert a new module. Step 5) Access the reference option under the tool tab and reference Microsoft HTML Object Library and Microsoft internet control.
Try this...
Dim elemCollection As IHTMLElementCollection
Set elemCollection = objDoc.frames("iFrameID").document.all
Debug.Print elemCollection.Item("pagemenuli-adv").innerText
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With