无法使用vba从网站填充结果

时间:2019-03-07 05:11:38

标签: html vba web-scraping internet-explorer-11

我已经使用IE在vba中创建了一个脚本,以便在从下拉列表中选择选项后显示内容。在网站的目标页面中,有一个名为Type的下拉列表。当我单击Type时,那里几乎看不到任何选项,我想单击其中的corporate bond。生成结果后,我想解析WKN

我尝试通过单击下拉菜单并从中选择一个选项来使用当前脚本填充结果。但是,它单击下拉列表并选择所需的选项,但在InternetExplorer中无法产生任何结果。

如何从下拉菜单中选择一个选项来填充和解析结果?

到目前为止我的尝试:

Sub SelectItem()
    Const link = "https://www.boerse-stuttgart.de/en/tools/product-search/bonds/"
    Dim IE As New InternetExplorer, Html As HTMLDocument, post As Object, T As Date

    With IE
        .Visible = True
        .navigate link
        While .Busy Or .readyState < 4: DoEvents: Wend
        Set Html = .document
    End With

    Application.Wait Now + TimeValue("00:00:15")
    Html.querySelector("#bsg-filters-btn-bgs-filter-3 .bsg-btn__icon").Click
    Application.Wait Now + TimeValue("00:00:05")
    Html.querySelector("#bsg-filters-menu-bgs-filter-3 .bsg-scrollbox label[for='bsg-checkbox-3053']").Click
    Application.Wait Now + TimeValue("00:00:05")
    Html.querySelector(".bsg-dropdown__footer button[type='button']").Click
End Sub

1 个答案:

答案 0 :(得分:2)

您熟悉定时循环,因此可以将超时添加到以下内容中。我使用css选择器来定位目标元素。我监视回旋圆的消失,以作为在各个点的页面加载的度量。最后,我监视结果项是否存在nodeList节点。

Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub GetCodes()
    Dim ie As New InternetExplorer, btn As Object, items As Object, i As Long
    With ie
        .Visible = True
        .Navigate2 "https://www.boerse-stuttgart.de/en/tools/product-search/bonds"

        While .Busy Or .readyState < 4: DoEvents: Wend

        Do
        Loop Until .document.querySelectorAll(".bsg-loader-ring__item").Length = 0


        .document.querySelector("#bsg-filters-btn-bgs-filter-3").Click
        .document.querySelector("#bsg-checkbox-3053").Click

        Set btn = .document.querySelector("#bsg-filters-menu-bgs-filter-3 .bsg-btn__label")

        Do
        Loop While btn.innerText = "Close"

        btn.Click

        Do
        Loop Until .document.querySelectorAll(".bsg-loader-ring__item").Length = 0
        Dim count As Long
        Do
            On Error Resume Next
            Set items = .document.querySelectorAll(".bsg-table__tr td:first-child")
            count = items.Length
            On Error GoTo 0
        Loop While count = 0

        For i = 0 To items.Length - 1
            Debug.Print items.item(i).innerText
        Next
        .Quit
    End With
End Sub