I am trying to use VBA code to click a "Go" button on a website. This is the source code.
<div class="actions">
<a id="go" href="javascript:void(null);" title="Go"><img src="/images/button-go-smaller.png"></a>
<a id="reset" href="javascript:void(null);" title="Reset All Fields"><img src="/images/button-reset_all.png"></a>
</div>
This is my VBA code:
For Each obj In objCollection
If objCollection(i).ID = "go" Then
Set objElement = obj
Exit For
End If
Next obj
objElement.Click
However, on the objElement.Click line, I get an error 91, which means that the "go" action cannot be found. Why is that, and how can I access the go button?
What about...
Dim objCollection As Object
Set objCollection = IE.Document.getElementsbyTagName("a")
For Each obj In objCollection
If obj.ID = "go" Then
obj.Click
Exit For
End If
Next obj
If you have an html object having an Id
you can get it direcly with something like this:
Dim GoObj As Object
Set GoObj = IE.Document.getElementbyId("go")
'Only if it was found you click it
If not GoObj is Nothing then
GoObj.Click
End If
Pay attention to the difference between element in getElementbyId
and element s in getElementsByTagName
This is a great example of how to do exactly that.
Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub Login_2_Website()
Dim oHTML_Element As IHTMLElement
Dim sURL As String
On Error GoTo Err_Clear
sURL = "https://www.google.com/accounts/Login"
Set oBrowser = New InternetExplorer
oBrowser.Silent = True
oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True
Do
' Wait till the Browser is loaded
Loop Until oBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = oBrowser.Document
HTMLDoc.all.Email.Value = "sample@vbadud.com"
HTMLDoc.all.passwd.Value = "*****"
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
' oBrowser.Refresh ' Refresh If Needed
Err_Clear:
If Err <> 0 Then
Debug.Assert Err = 0
Err.Clear
Resume Next
End If
End Sub
Read more at: http://vbadud.blogspot.com/2009/08/how-to-login-to-website-using-vba.html#XwwFWylLQi9rjILC.99
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.