我已经使用IE在vba中创建了一个脚本,以填充网页中的少量输入以填充特定项目。
以下是我希望脚本执行的步骤:
Buy Bricks
submit
按钮Element/design number
框中的乐高积木,例如4219725
我的脚本可以满足前两个要求(单击“提交”按钮除外)。即使正确填写了输入框,“提交”按钮仍显示为灰色。单击submit
按钮后,我希望满足第三个要求。
我的尝试:
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
如何激活显示为灰色的提交按钮以启动单击?
答案 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