.Click action not doing anything - IE Simulation

冷暖自知 提交于 2019-12-12 02:34:14

问题


I'm trying to simulate the interaction with Google through the IE app and going through the DOM to get the classes I need and all is fine, stepping though the code, except the .Click action which doesn't cause a crash but it doesn't do anything (page doesn't navigate) - Code and screenshot of HTML below:

Option Explicit

Private Sub Test_Automation()
Dim ie, doc, eInput, eButton, eButtons As Object
Dim sURL, sTest As String

Set ie = CreateObject("internetexplorer.application")
sURL = "https://www.google.co.uk/?gfe_rd=cr&ei=IpDvWK72LsjCaJCbjKAL&gws_rd=ssl"
sTest = "Test"

With ie
    .Visible = True
    .Navigate sURL
End With

Do While ie.Busy Or ie.readyState <> 4
    DoEvents
Loop

Set doc = ie.document

Set eInput = doc.getElementByid("lst-ib")
Set eButtons = doc.getElementsByTagName("input")

eInput.Value = sTest

For Each eButton In eButtons
    If (eButton.getattribute("name") = "btnK") Then
        eButton.Click
        Exit For
    End If
Next

End Sub

Any advise on what I'm doing wrong would be great!


回答1:


You can get rid of your For...Next loop at the bottom and replace it with this to click the button:

doc.forms(0).submit

The 0 can be changed to another number (such as 1 or 2) to click on a different button. If there are multiple buttons on a page that can be clicked on it will just take some trial and error to find out which number matches the button you want to click.



来源:https://stackoverflow.com/questions/43397401/click-action-not-doing-anything-ie-simulation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!