我想使用VBA点击网页按钮。我尝试了几个选项(请参阅下面的代码),但它们似乎都没有用。如果不是单击而是使用焦点,它会选择我要推送的按钮,但是click命令不起作用。我可以尝试哪些其他方法?
Sub SubmitInfo()
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "http://www.ecotransit.org/calculation.en.html"
Do While IE.readyState <> 4: DoEvents: Loop
Application.Wait (Now + TimeValue("0:00:02"))
' 1st option
IE.document.getElementById("calculationBT").Click
' 2nd option
IE.document.getElementsByClassName("formGreenButton")(0).Click
' 3rd option
Set htmlColl = IE.document.getElementsByTagName("input")
Do While IE.document.readyState <> "complete": DoEvents: Loop
For Each htmlInput In htmlColl
If Trim(htmlInput.Type) = "submit" Then
htmlInput.Click
Exit For
End If
Next htmlInput
' 4th option
Set htmlColl = IE.document.getElementsByTagName("input")
Do While IE.document.readyState <> "complete": DoEvents: Loop
For Each htmlInput In htmlColl
If Trim(htmlInput.Value) = "CALCULATE" Then
htmlInput.Click
Exit For
End If
Next htmlInput
End Sub
下面是我要点击的按钮HTML代码
<input id="calculationBT" type="submit" class="formGreenButton" value="CALCULATE" />
答案 0 :(得分:0)
IE.document.getElementById("calculationBT").Click
应该完成这项工作,但你必须确保只有一个id出现,在这种情况下应该是这样。