Search code examples
internet-explorerbuttonvbscriptgetelementbyid

Clicking buttons in IE with VBScript on CRA website


I’m very new to VBScript and I’m trying to automate some payroll calculations through the CRA website http://www.cra-arc.gc.ca/esrvc-srvce/tx/bsnss/pdoc-eng.html in excel. I’m been trying to figure this out for few days now with no luck. I think my problem is very simple: I’m trying to click on the “I accept button” at the bottom of the page of the website above.

The code I’ve written is below but for the following line I don’t know how to actually click the button. I always get an error here whatever I try.

 objIE.Application.document.getElementById("??????").Click

The HTML code below has no ID. I’ve also tried various other things like getElementByType, getElementByValue with no luck

VBScript

Sub test()

Set objIE = CreateObject("InternetExplorer.Application")

objIE.Application.Visible = True
objIE.Application.navigate "http://www.cra-arc.gc.ca/esrvc-srvce/tx/bsnss/pdoc-eng.html"

Do While objIE.Application.Busy Or _
    objIE.Application.readyState <> 4
    DoEvents
Loop

objIE.Application.document.getElementById("??????").Click

Do While objIE.Application.Busy Or _
    objIE.Application.readyState <> 4
    DoEvents
Loop

End Sub

HTML Code

<div class="alignCenter">
<input type="button" title="I accept - Begin the calculation" value="I accept" onclick="parent.location='https://apps.cra-arc.gc.ca/ebci/rhpd/startLanguage.do?lang=English'" />
<input type="button" title="I do not accept - Return to Payroll" value="I do not accept" onclick="parent.location='/tx/bsnss/tpcs/pyrll/menu-eng.html'" />
</div>

I would appreciate someones help.

Thanks,

Matthew


Solution

  • Note that VB/VBA DoEvents function not available in VBScript. However, here is working code.

    Call test
    
    Sub test()
    
        With CreateObject("InternetExplorer.Application")
    
            .Visible = True
            .Navigate "http://www.cra-arc.gc.ca/esrvc-srvce/tx/bsnss/pdoc-eng.html"
    
            Do While .Busy Or .readyState <> 4
                WScript.Sleep 50
            Loop
    
            Set oInputs = .Document.getElementsByTagName("input")
    
            For Each elm In oInputs
                If elm.Value = "I accept" Then
                    elm.Click
                    Exit For
                End If
            Next
    
            Do While .Busy Or .readyState <> 4
                WScript.Sleep 50
            Loop
    
            End With
    
    End Sub