在即时窗口中显示IE中元素的innerHTML

时间:2017-09-01 08:35:48

标签: html vba internet-explorer web-scraping

我正在使用VBA导航到IE中的网页。网页的元素在几秒钟后动态加载。 我正在尝试使用该网页中包含链接的树结构。所以我试图使用以下代码查看内部HTML,

    Sub treemenutest()

    Dim ie As SHDocVw.InternetExplorerMedium
    Dim doc As MSHTML.HTMLDocument
    Dim elements3 As MSHTML.IHTMLElementCollection
    Dim nameValueInput3 As MSHTML.HTMLInputElement
    Dim LIS As IHTMLElementCollection
    Dim LI As IHTMLElement

    Set ie = New SHDocVw.InternetExplorerMedium
    ie.Visible = True
    ie.navigate Sheet1.TextBox1.Value 'web page URL

    Do Until Not ie.Busy And ie.readyState = 4
        DoEvents
    Loop

    Set doc = ie.document

    Set elements3 = doc.getElementsByName("OK")
    If Not elements3 Is Nothing Then
        Set nameValueInput3 = elements3(1)
           If Not nameValueInput3 Is Nothing And nameValueInput3.Type = "submit" Then _
                   nameValueInput3.Click
           End If

    Set LIS = ie.document.getElementsByTagName("a")
    For i = 0 To LIS.Length - 1
    Debug.Print LI.innerHTML
    Next

    End Sub

Debug.Print没有在即时窗口中显示任何信息。编码是否有问题,还是因为网页的某些内容是动态的?

任何信息都会有所帮助。

1 个答案:

答案 0 :(得分:0)

我会使用foreach循环

For each LI in LIS
    Debug.Print LI.innerHTML 
Next

但你也可以迭代这样的标签:

For i = 0 To LIS.Length - 1
    Debug.Print LIS.item(i).innerHTML
Next