chappemo
chappemo

Reputation: 27

Clicking a button in Internet Explorer

I am trying to create some VBA code that clicks a button with the following HTML:

<button class="button small btnViewAction atlas--ui-button" type="button" data-tracking-value="Action" data-tracking-label="View Action" data-tracking-## Heading ##category="Reconciliations" data-attribs-childassignmentid="661" data-attribs-reconciliationid="145870" data-attribs-assignmenttype="A" data-attribs-assignmentid="1223" value="undefined" data-columnname="action">Edit</button>

Is there a way to reference this button?

Upvotes: 1

Views: 935

Answers (2)

ASH
ASH

Reputation: 20302

Here is a very generic sample of how to do this.

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

Try the code above and see if you can implement it for your specific situation. If you need a nudge, post back, with additional requirements.

Upvotes: 0

Robert Todar
Robert Todar

Reputation: 2145

Without seeing more of the HTML I can't say this will work for sure, but it should give you a good guideline for what you can do!

Make sure you update ie.navigate to your site.

This is a late binding example, you can also set a reference to Microsoft Internet Controls.

Sub clickButton()

    Dim ie As Object
    Dim Btn As Object

    'Late Binding
    Set ie = CreateObject("InternetExplorer.Application")

    On Error GoTo Catch
    ie.Visible = True
    ie.navigate "https://yourwebsite.com/"

    While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend

    'LOOP EACH CLASS ELEMENT
    For Each Btn In ie.Document.getElementsByClassName("button")

        If Btn.innertext = "Edit" Then
            Btn.Click
        End If

    Next Btn

    'CLOSE INSTANCE OF IE
Catch:
    ie.Quit
    Set ie = Nothing

End Sub

Upvotes: 1

Related Questions