无法让我的脚本停止打印错误的结果

Unable to make my script stop printing wrong result

我在 vba 中创建了一个脚本,使用 IE 在网页中填写一些输入,以便到达一个新页面,根据在输入框中输入一些值来检查某些项目的可用性。

引导您完成:脚本当前正在做什么:

  1. Select Buy Bricks 来自着陆页
  2. 输入年龄30和国家United Kingdom然后点击submit按钮
  3. 在下一页的 Element/design 数字框中输入乐高积木的唯一标识号以填充结果。

My script can satisfy all the requirements stated above. However, when I try with three different numbers, as in 4219725,765467 and 230223 I can see that the one in the middle 765467 doesn't populate any result but It prints the result of it's earlier number.

所有三个数字都已在我下面的脚本中的 for loop 中使用。

如何让我的脚本在没有结果时不打印任何内容而不是打印错误的结果?

Site address

到目前为止我的脚本:(无法排除硬编码延迟)

Sub GetDetails()
    Const timeOut = 10
    Dim IE As New InternetExplorer, Html As HTMLDocument
    Dim elem As Object, post As Object, inputNum As Variant
    Dim ageInput As Object, itm As Object, T As Date

    With IE
        .Visible = True
        .navigate "https://www.lego.com/en-gb/service/replacementparts"
        While .Busy Or .readyState < 4: DoEvents: Wend
        Set Html = .document
        Dim event_onChange As Object
        Set event_onChange = .document.createEvent("HTMLEvents")
        event_onChange.initEvent "change", True, False

        Html.querySelectorAll(".arrow-list-info")(2).Click

        Do: Set ageInput = Html.querySelector("input[id*='How old']"): DoEvents: Loop While ageInput Is Nothing
        ageInput.innerText = 30
        Html.querySelector("[label='United Kingdom").Selected = True
        Html.querySelector("select").dispatchEvent event_onChange
        Html.querySelector("[ng-click='startFlow()'").Click

        While .Busy Or .readyState < 4: DoEvents: Wend
        Set Html = .document

        For Each inputNum In [{4219725,765467,230223}]
            T = Timer
            Do: Set post = Html.querySelector("[placeholder='Element/design number']"): DoEvents: Loop While post Is Nothing
            post.ScrollIntoView
            post.Focus
            post.innerText = inputNum
            Html.querySelector("button[ng-click='searchItemNumber()']").Click

            'Can't kick out this hardcoded delay
            Application.Wait Now + TimeValue("00:00:02")

            Do
                Set elem = Html.querySelector("div.list-item")
                If Timer - T > timeOut Then Exit Do
                DoEvents
            Loop While elem Is Nothing

            Set itm = Html.querySelector("h6.title")
            If Not itm Is Nothing Then
                Debug.Print itm.innerText
            Else:
                Debug.Print "Found Nothing"
            End If
        Next inputNum
        Stop
    End With
End Sub

所以这需要整理但确实如此。我摆脱了显式等待并添加了等待微调器消失的时间。对于无结果部分,我在 html 中查找未找到的其他元素。

Option Explicit
Public Sub GetDetails()
    Const timeOut = 10
    Dim ie As New InternetExplorer, html As HTMLDocument
    Dim elem As Object, post As Object, inputNum As Variant
    Dim ageInput As Object, itm As Object, t As Date

    With ie
        .Visible = True
        .navigate "https://www.lego.com/en-gb/service/replacementparts"

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

        Set html = .document

        Dim event_onChange As Object
        Set event_onChange = .document.createEvent("HTMLEvents")
        event_onChange.initEvent "change", True, False

        html.querySelectorAll(".arrow-list-info")(2).Click

        Do: Set ageInput = html.querySelector("input[id*='How old']"): DoEvents: Loop While ageInput Is Nothing

        ageInput.innerText = 30
        html.querySelector("[label='United Kingdom']").Selected = True
        html.querySelector("select").dispatchEvent event_onChange
        html.querySelector("[ng-click='startFlow()']").Click

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

        For Each inputNum In [{4219725,765467,230223}]

                Do: Set post = .document.querySelector("[placeholder='Element/design number']"): DoEvents: Loop While post Is Nothing

                post.Focus
                post.innerText = inputNum
                html.querySelector("button[ng-click='searchItemNumber()']").Click

                Do
                Loop While .document.querySelectorAll(".basic-search-btn .icon-spinner-arrows").Length > 0

            t = Timer
            Do
                Set elem = html.querySelector("div.list-item")
                If Timer - t > timeOut Then Exit Do
                DoEvents
            Loop While elem Is Nothing

            Set elem = Nothing
            Set itm = html.querySelector("h6.title")

            If html.querySelectorAll(".alert.alert-info.margin-top.ng-hide").Length = 1 Then
                Debug.Print "Found nothing"
            Else
                Debug.Print itm.innerText
            End If
            Set itm = Nothing
        Next inputNum
        ie.Quit
    End With
End Sub