Reputation: 290
I am trying to click a button on a webpage using VBA. The button is within a <button>
tag, as in the code snipped below:
<div class="search-options-container">
<button class="aui-item aui-button aui-button-subtle search-button"
type="button" original-title="Search for Issues">
<span class="aui-icon aui-icon-small aui-iconfont-search">Search</span></button>
</div>
The code I am using so far is:
Dim ieApp As New SHDocVw.InternetExplorer
Dim ieElement As Object
Dim oHTML_Element As IHTMLElement
...
Do While ieApp.Busy And Not ieApp.readyState = READYSTATE_COMPLETE
DoEvents
Loop
For Each oHTML_Element In ie.Document.getElementsByName("button")
Debug.Print ("REACHED")
If oHTML_Element.className = "aui-item aui-button aui-button-subtle search-button" Then
oHTML_Element.Click
End If
Next
Which gives me an object required error. I also tried using:
ieElement = ieApp.Document.getElementsByTagName("button")
Which also gives an object required error.
Edit: Corrected the search string as pointed out by user Jordan. The Debug.Print does not execute, so the error probably already arises, when looking for the element with .getElementsByName . The script is already able to open the page and enter text in a search box before clicking the button.
Upvotes: 1
Views: 815
Reputation:
Here is the classic example for this kind of question.
http://vbadud.blogspot.com/2009/08/how-to-login-to-website-using-vba.html
And here is all the code.
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 = "[email protected]"
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
Upvotes: 1
Reputation: 4514
Firstly, you are searching for elements in an index that doesn't exist. In the html example you have provided there is not an element collection named "button".
Secondly, the class name you are searching for in your current code is:
"aui-item aui-button-subtle search-button"
However in your html example the class name of the button is:
"aui-item aui-button aui-button-subtle search-button"
Try replacing your For
loop with:
ieApp.Document.getElementsbyClassName("aui-item aui-button aui-button-subtle search-button")(0).Click
Upvotes: 1