CluelessProgrammer
CluelessProgrammer

Reputation: 1

Accessing Embedded HTML Elements with VBA Web Automation

I just picked up VBA a few weeks ago and I'm working on a project that will be able to retrieve info from a web page for me. As is, the task can take me up to 5 hours to complete manually. I'm trying to click a link on a .jsp page, located on an embedded webpage that uses javascript to navigate within the frames, from a VBA script running in excel. Unfortunately the webpage has strict rules about linking from outside sources, but the webpage hierarchy is like this:

<html>
    <head></head>
    <frameset>
        <frame name="DontCare1"></frame>
        <frameset>
            <frame name="OtherFrame1"></frame>
            <frame name="FrameName">
               #document<html></html> 
               <!--Inspect Page shows that the html is inside the #document tag-->
            </frame>
            <frame name="OtherFrame2"></frame>
        </frameset>
        <frame name="DontCare2"></frame>
    </frameset>
</html>

Now, I know my code is setup to correctly manipulate web pages because before getting to this point it accesses a standard page and can log the user in by entering their info and clicking submit so I'm fairly certain all my libraries and references are in order.

I've got a poor method to get the deepest frame as shown here:

Dim theFrame As HTMLIFrame
Dim l2 As Variant, l3 As Variant, l4 As Variant
For Each l2 In .Document.getElementsByTagName("frameset")
            For Each l3 In l2.getElementsByTagName("frameset")
                For Each l4 In l3.getElementsByTagName("frame")
                    If l4.Name = "FrameName" Then
                        Set theFrame = l4 ' Find HTML page in this frame
                    End If
                Next l4
            Next l3
        Next l2

I have verified that it is setting theFrame to the correct frame. Now I thought that something like:

For Each l2 In theFrame.getElementsByTagName("a")
            If l2.href="LinkIWantToClick" Then
                l2.Click
                Exit For
            End If
        Next l2

Would work but theFrame.getElementsByTagName("a") returns an empty collection or occasionally throws runtime errors 91 or 438. I've searched just about every combination I can think of that may have a solution I could adapt to use but no such luck so far.

I've got more functionality to build into this after getting the link clicked, but I think after I know how to access that link I should be good to access anything else located on these pages. Still new to this language and don't necessarily know what I'm doing yet, hopefully I didn't miss an obvious mistake, so any advice you could give would be helpful.

Upvotes: 0

Views: 978

Answers (1)

Tom K
Tom K

Reputation: 11

Tim Williams is right, there will be another document embedded in the frame. I was having a similar problem and was able to access the elements I needed using contentDocument.

For Example:

TheFrame.contentDocument.getElementsByTagName("a")

Upvotes: 1

Related Questions