通过遍历所有innerHTML来选择按钮

时间:2020-10-06 18:48:51

标签: vba internet-explorer

我试图使用queryselector获取所有按钮,并通过遍历并查找innerHTML来选择特定按钮。

找到特定按钮后,当点击事件通过时,什么也没有发生。

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://www.spirit.com/"
Do Until IE.readyState = 4: Application.Cursor = xlDefault: DoEvents: Loop

Application.Wait (Now + TimeValue("00:00:03"))
IE.document.querySelector("label[for='radio-oneWay']").Click

Application.Wait (Now + TimeValue("00:00:03"))
IE.document.querySelector("input[id='flight-OriginStationCode']").Click
Set elems = IE.document.getElementsByTagName("button")

For Each elem In elems
    If (elem.innerHTML) = " Aguadilla, Puerto Rico (BQN) " Then
        elem.Click
        Exit For
    End If
Next elem

1 个答案:

答案 0 :(得分:0)

可以使用SendKeys模拟输入来分配两个机场方框,然后将触发点击事件。日期输入和其他文本框将分别设置。这是实现此功能的代码:

Private Sub CommandButton1_Click()

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://www.spirit.com/"
Do Until IE.readyState = 4: Application.Cursor = xlDefault: DoEvents: Loop

Application.Wait (Now + TimeValue("00:00:03"))
IE.document.querySelector("label[for='radio-oneWay']").Click

Application.Wait (Now + TimeValue("00:00:03"))

Set elems = IE.document.getElementsByTagName("button")

Set txt1 = IE.document.getElementsByName("originStationCode")(0)
txt1.Focus
SendKeys ("BQN")
Application.Wait (Now + TimeValue("00:00:01"))

Set txt2 = IE.document.querySelector("input[id='flight-DestinationStationCode']")
txt2.Focus
SendKeys ("BDL")
Application.Wait (Now + TimeValue("00:00:01"))

Application.Wait (Now + TimeValue("00:00:01"))
IE.document.querySelector("input[id='flight-Date']").Value = "2020/10/09-2020/10/19"
IE.document.querySelector("input[id='promoCode']").Value = "xx"
      
IE.document.querySelector("button[type='submit']").Click
End Sub

结果:

enter image description here