首页 文章

使用VBA Web Automation访问嵌入式HTML元素

提问于
浏览
0

几周前我刚拿起VBA,我正在开发一个能够从网页中检索信息的项目 . 这样,任务可能需要5个小时才能手动完成 . 我正在尝试点击位于嵌入式网页上的.jsp页面上的链接,该网页使用javascript在框架内导航,从excel中运行的VBA脚本 . 不幸的是,网页有关于从外部源链接的严格规则,但网页层次结构如下:

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

现在,我知道我的代码设置为正确操作网页,因为在此之前它访问标准页面并可以通过输入信息并单击提交来登录用户,所以我相当确定我的所有库和引用都在订购 .

我有一个糟糕的方法来获得最深的帧,如下所示:

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

我已经验证它正在将帧设置为正确的帧 . 现在我觉得有点像:

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

会工作,但theFrame.getElementsByTagName(“a”)返回一个空集合或偶尔抛出运行时错误91或438.我已经搜索了我能想到的每一个组合,可能有一个我可以适应使用的解决方案,但没有这样的运气至今 .

在点击链接后我有更多的功能可以构建到这个,但我想在我知道如何访问该链接后,我应该很好地访问这些页面上的任何其他内容 . 这个语言还是新手,并不一定知道我在做什么,希望我没有错过一个明显的错误,所以你能给出的任何建议都会有所帮助 .

1 回答

  • 1

    Tim Williams是对的,框架中会嵌入另一个文件 . 我遇到了类似的问题,并且能够使用 contentDocument 访问我需要的元素 .

    例如:

    TheFrame.contentDocument.getElementsByTagName("a")
    

相关问题