我在vba中创建了一个脚本,使用IE填充网页中的一些输入以填充某个项目。
以下是我希望我的脚本执行的步骤:
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
如何激活灰色的提交按钮以启动点击它?
将更改事件添加到select元素
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