无法单击网页中的某些灰色提交按钮

时间:2019-03-09 19:00:16

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

我已经使用IE在vba中创建了一个脚本,以填充网页中的少量输入以填充特定项目。

以下是我希望脚本执行的步骤:

  1. 从登录页面中选择Buy Bricks
  2. 输入年龄(35)和国家(英国),然后单击submit按钮
  3. 在下一页上,输入唯一标识号 Element/design number框中的乐高积木,例如4219725

我的脚本可以满足前两个要求(单击“提交”按钮除外)。即使正确填写了输入框,“提交”按钮仍显示为灰色。单击submit按钮后,我希望满足第三个要求。

Site address

我的尝试:

Sub GetDetails()
    Dim IE As New InternetExplorer, I As Long
    Dim Html As HTMLDocument, post As Object, ageInput As Object

    With IE
        .Visible = True
        .navigate "https://www.lego.com/en-gb/service/replacementparts"
        While .Busy Or .readyState < 4: DoEvents: Wend
        Set Html = .document
        Html.querySelectorAll(".arrow-list-info")(2).Click

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

        Do: Set post = Html.querySelector("select[ng-model='country']"): DoEvents: Loop While post Is Nothing
        For I = 0 To post.Length - 1
            If InStr(post(I).innerText, "United Kingdom") > 0 Then post(I).Selected = True: Exit For
        Next I
    End With
End Sub

如何激活显示为灰色的提交按钮以启动单击?

1 个答案:

答案 0 :(得分:1)

将更改事件添加到选择元素

Option Explicit
Public Sub GetDetails()
    Dim IE As New InternetExplorer, html As HTMLDocument, ageInput As Object

    With IE
        .Visible = True
        .Navigate2 "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
        IE.document.querySelector("select").dispatchEvent event_onChange
        IE.document.querySelector("[ng-click='startFlow()'").Click

        Do: Loop While IE.document.querySelectorAll("[ng-model=itemNumber]").Length = 0
        IE.document.querySelector("[ng-model=itemNumber]").Focus
        IE.document.querySelector("[ng-model=itemNumber]").Value = "4219725"
        Stop
    End With
End Sub