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

问题描述 投票:1回答:1

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

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

  1. 从登录页面选择Buy Bricks
  2. 输入年龄30和国家United Kingdom然后点击submit按钮
  3. 在下一页上,在Element/design数字框中输入乐高积件的唯一标识号以填充结果。

我的脚本可以满足上述所有要求。然而,当我尝试使用三个不同的数字时,如4219725765467230223,我可以看到中间765467中的那个没有填充任何结果,但它打印出它的早期数字的结果。

所有这三个数字都在我下面的脚本中用于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
vba web-scraping internet-explorer-11
1个回答
4
投票

所以这需要整理,但这样做。我摆脱了明确的等待,并添加了等待微调器消失。对于无结果部分,我找不到找到时在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
© www.soinside.com 2019 - 2024. All rights reserved.