【问题标题】:Unable to make my script stop printing wrong result无法让我的脚本停止打印错误的结果
【发布时间】:2019-07-31 22:40:56
【问题描述】:

我在 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 大师。在您打印“Found Nothing”的情况下,您是否需要在 Else 之后使用 :
  • 不,我不需要,但我需要在没有结果时在电子表格中写一些东西@Priyank Panchal。谢谢。
  • 手动输入有冲突的数字会产生什么结果?
  • No results@Luuklag.
  • 尝试用If Not IsNull(itm) Then替换If Not itm Is Nothing Then,看看是否有帮助

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


【解决方案1】:

所以这需要整理,但确实做到了。我摆脱了显式等待并添加了等待微调器消失的时间。对于无结果部分,我会在 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

【讨论】:

  • 您能帮我了解一下您是如何抓住旋转器@QHarr 的吗?当你使用这条线.querySelectorAll(".basic-search-btn .icon-spinner-arrows")时,你一定注意到了spinner的下落。我尝试使用 chrome 开发工具,但似乎没有找到它。开发工具中的哪个选项卡负责检测?提前致谢。
  • 难度很大。我不得不屏幕抓取。它仅在呼呼时出现。
  • 是的,我明白这一点。通常按F8 可以阻止那件事,但问题是我看不到任何旋转。
  • 我将开发工具与(windows + 箭头键)网页并排排列,同时使用 f8 单步执行。单击按钮和屏幕截图。可能更容易使用屏幕录像机,但屏幕抓取工作。
  • 微调器的说明可能也在其他地方。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-19
  • 2012-04-08
  • 1970-01-01
  • 2019-04-01
  • 1970-01-01
  • 2021-09-18
相关资源
最近更新 更多